Jav*_*vid 4 delphi xor-drawing
我正在寻找一种用反色绘制文字的方法.对于形状,我们TPenMode可以将其设置为pmNot,但我们不能对文本执行此操作.我怎么能这样做呢?
这样做:
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;
Run Code Online (Sandbox Code Playgroud)
例:
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;
Run Code Online (Sandbox Code Playgroud)
您可能还想禁用ClearType.为此,我向您推荐以前的SO问题.