Rob Stow <rob.s...@cnnsimail.com> schreef in berichtnieuws
39A82824.5A534...@cnnsimail.com...
Quote
> Dirk Claessens wrote:
[snip]
Quote
> Then the API function is buggy because it doesn't take line breaks
> into account.
Rob,
As Bruce and I tried to explain,
TextWidth() ->WIN32API.GetTextExtentPoint() do
exactly what they were _designed_ for, i.e. : _single_ line text output on
a _canvas_. Therefore, cr/lf/backspace/etc.. will not be taken into
account.
See what TextOut() actually does in Graphics.pas :
------------------------------------------------------------
procedure TCanvas.TextOut(X, Y: Integer; const Text: String);
begin
Changing;
RequiredState([csHandleValid, csFontValid, csBrushValid]);
if CanvasOrientation = coRightToLeft then Inc(X, TextWidth(Text) + 1);
Windows.ExtTextOut(FHandle, X, Y, FTextFlags, nil, PChar(Text),
*****See? only string _length_ is taken into account here*****
Length(Text), nil);
MoveTo(X + TextWidth(Text), Y);
Changed;
end;
[snip]
Quote
> If I do something like
> TStaticText.Caption := 'line 1'+#13+'line 2'+#13+'line 3'
> the TStaticText WILL take the line breaks into account when
> it draws the text on its canvas.
[snip]
TStaticText does not _have_ a canvas, that's the whole point!
TstaticText is derived from TCustomStaticText, which indeed will
take line breaks into account. Extract from StdCtrls.pas:
Adjustbounds() is called from TextChanged()
----------------------------------------
procedure TCustomStaticText.AdjustBounds;
var
DC: HDC;
SaveFont: HFont;
TextSize: TSize;
begin
if not (csReading in ComponentState) and FAutoSize then
begin
DC := GetDC(0);
SaveFont := SelectObject(DC, Font.Handle);
=========================================Both width and height here !
GetTextExtentPoint32(DC, PChar(Caption), Length(Caption), TextSize);
=======================================================
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
SetBounds(Left, Top,
TextSize.cx + (GetSystemMetrics(SM_CXBORDER) * 4),
TextSize.cy + (GetSystemMetrics(SM_CYBORDER) * 4));
end;
end;
As Bruce suggested, you might want to try DrawText(), or, if your app must
run under NT,DrawTextEx().
Always bear in mind that Borland for the development of Delphi/Builder/etc..
must comply to the WINAPI as designed by MS, whether they ( or you, or me )
like it or not.
--
Regards,
Dirk Claessens
---------------------------------------------------------
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------