How to draw “not” colored text?

I am looking for a way to draw inverted text. For shapes, we have a TPenMode that can be set to pmNot , but we cannot do this for text. How can i do this?

+4
source share
2 answers

It does:

 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. For this, I refer you to the previous SO question .

+9
source

GDI text is not drawn with a pen. Have you considered drawing text in a temporary bitmap and copying using BitBlt ? Probably a combination of dwRop raster operations that might get the effect you're looking for.

+2
source

Source: https://habr.com/ru/post/1340480/


All Articles