Is there an analogue of Delphi TextRect
in GDI? I looked at DrawText, DrawTextEx
, but I didn't find what I needed. I need to draw a percent text of a progress bar that is divided in two color parts, e.g. the left part of the text is black, the right one is white. So as usually in all progress bars.
Thanks for your answers!
You are looking for the ExtTextOut
function.
Sample:
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;
But I think that what you really want to do, is to draw '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;
(source: rejbrand.se)