【问题标题】:How to draw a "not" colored text?如何绘制“非”彩色文本?
【发布时间】:2011-02-20 05:28:56
【问题描述】:

我正在寻找一种用反转颜色绘制文本的方法。 对于形状,我们有 TPenMode 可以设置为 pmNot,但我们不能对文本执行此操作。我该怎么做呢?

【问题讨论】:

    标签: delphi xor-drawing


    【解决方案1】:

    这样做:

    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;
    

    例子:

    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;
    

    您可能还想禁用 ClearType。为此,我将您推荐给a previous SO question

    【讨论】:

      【解决方案2】:

      GDI 文本不是用钢笔绘制的。您是否考虑过将文本绘制到临时位图,并使用BitBlt 进行复制?可能有dwRop 光栅操作的组合可以获得您正在寻找的效果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多