Realbitt
Realbitt

Reputation: 408

Why does DrawText only print the first four characters of my string?

i have problem with Canvas DrawText , i got just first 4 chars from text i want to show .

as example i have text 'offline' it will show 'offl' .

what the wrong in this code .

the code

procedure TStatusCombo.Paint ;
var DrawRect : TRect ;
    StatusColor : TColor ;
    iTextWidth : Integer ;
    r : TRect ;
begin
DrawRect := ClientRect ;

//colors
Canvas.Brush.Style := bsClear ; //transparent background
Canvas.Brush.Color := Tcolor($4D4D4D) ;
Canvas.Pen.Color :=  Tcolor($4D4D4D)   ;

Canvas.RoundRect(DrawRect.Left , DrawRect.Top ,
            DrawRect.Right , DrawRect.Bottom , 15, 15);

//Drawing
//SetRect(DrawRect, DrawRect.Left+3, DrawRect.Top+3, DrawRect.Right-3, DrawRect.Bottom-3);

case ChatStatus of
  sNormal: StatusColor  :=   TColor($00FF78) ;
  sOnline: StatusColor  :=   TColor($00FF78) ;
  sBusy: StatusColor  :=   TColor($00FF78) ;
  sAway: StatusColor  :=   TColor($00FF78) ;
  sOffline:StatusColor  :=   TColor($00FF78) ;
end;
Canvas.Brush.Color := StatusColor ;

Canvas.RoundRect(DrawRect.Right - 20 , DrawRect.Top +4 ,
            DrawRect.Right -10  , DrawRect.Bottom -4  , 5, 5);


FCaption := FStatusText[ChatStatus];
Canvas.Brush.Style := bsClear ;
Canvas.Font.Color := clWhite ;


DrawText(Canvas.Handle,pchar(FCaption) ,sizeof(FCaption),DrawRect ,DT_VCENTER  or DT_CENTER or DT_SINGLELINE);




end;

Upvotes: 0

Views: 287

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109003

You have to use Length(FCaption) instead of SizeOf(FCaption).

Indeed, Length(FCaption) is the length of the string FCaption, while SizeOf(FCaption) is the size of the variable FCaption. Since FCaption is a string, it is (technically) a pointer (to the actual characters), and a pointer is four bytes long (in 32-bit applications). Hence, you only got the first four characters.

Upvotes: 7

Related Questions