Windows GDI中的Delphi TextRect

时间:2011-08-28 17:16:47

标签: delphi text gdi rect drawtext

GDI中是否有Delphi TextRect的类似物?我看了DrawText, DrawTextEx,但我找不到我需要的东西。我需要绘制一个进度条的百分比文本,该进度条分为两个颜色部分,例如文本的左侧部分是黑色,右侧是白色。通常在所有进度条中都是如此。

enter image description here

感谢您的回答!

1 个答案:

答案 0 :(得分:9)

您正在寻找ExtTextOut功能。

样品:

procedure TForm4.FormPaint(Sender: TObject);
const
  S = 'This is a sample text';
begin
  ExtTextOut(Canvas.Handle, 10, 10, ETO_CLIPPED,
    Rect(40, 10, 100, 100), PChar(S), length(S), nil)    
end;

但我认为你真正想做的是绘制'NOT-coloured text'

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 TForm4.FormPaint(Sender: TObject);
const
  S = 'This is a sample text';
var
  ext: TSize;
begin
  Canvas.Brush.Color := clBlack;
  Canvas.FillRect(Rect(0, 0, Width div 2, Height));
  Canvas.Brush.Color := clWhite;
  Canvas.FillRect(Rect(Width div 2, 0, Width, Height));
  ext := Canvas.TextExtent(S);

  DrawTextNOT(Canvas.Handle, Canvas.Font, S, (Width - ext.cx) div 2,
    (Height - ext.cy) div 2);
end;

Screenshot http://privat.rejbrand.se/nottext.png