How to draw a “not” colored text?

此生再无相见时 提交于 2019-12-06 05:34:17

问题


I'm looking for a way to draw text with inverted colors. For shapes, we have TPenMode that can be set to pmNot, but we can't do this for text. How can I do this instead?


回答1:


This does it:

procedure DrawTextNOT(const hDC: HDC; const Font: TFont; const Text: string; const X, Y: integer);
begin
  with TBitmap.Create do
    try
      Canvas.Font.Assign(Font);
      with Canvas.TextExtent(Text) do
        SetSize(cx, cy);
      Canvas.Brush.Color := clBlack;
      Canvas.FillRect(Rect(0, 0, Width, Height));
      Canvas.Font.Color := clWhite;
      Canvas.TextOut(0, 0, Text);
      BitBlt(hDC, X, Y, Width, Height, Canvas.Handle, 0, 0, SRCINVERT);
    finally
      Free;
    end;
end;

Example:

procedure TForm1.FormClick(Sender: TObject);
begin
  Canvas.Brush.Color := clRed;
  Canvas.FillRect(ClientRect);
  DrawTextNOT(Canvas.Handle, Canvas.Font, 'This is a test.', 20, 100);
//  DrawTextNOT(Canvas.Handle, Canvas.Font, 'This is a test.', 20, 100);
end;

You probably also want to disable ClearType. To do that, I refer you to a previous SO question.




回答2:


GDI text isn't drawn with a pen. Have you considered drawing the text to a temporary bitmap, and copying with BitBlt? There's probably a combination of the dwRop raster operations that can get the effect you're looking for.



来源:https://stackoverflow.com/questions/5053282/how-to-draw-a-not-colored-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!