为什么DrawText只打印字符串的前四个字符?

时间:2012-07-14 10:20:34

标签: delphi

我有Canvas DrawText的问题,我从我想要显示的文本中得到了前4个字符。

例如我的文字'离线'它会显示'offl'。

这段代码有什么不对。

代码

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;

1 个答案:

答案 0 :(得分:7)

您必须使用Length(FCaption)代替SizeOf(FCaption)

实际上,Length(FCaption)是字符串FCaption的长度,而SizeOf(FCaption)是变量FCaption的大小。由于FCaption是一个字符串,它(技术上)是指针(指向实际字符),指针长度为四个字节(在32位应用程序中)。因此,你只得到前四个字符。