Lazarus

Free Pascal => Beginners => Topic started by: TomTom on September 29, 2022, 11:23:06 am

Title: StatusBar panel DrawText special characters
Post by: TomTom on September 29, 2022, 11:23:06 am
Hi :) I have problem with displaying text using DrawText() on StatusBar panel. I wanted to change default text in one of Panels. It works ok but the text have some special characters and they are not displayed properly. What should I do ?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
  2.   Panel: TStatusPanel; const Rect: TRect);
  3. var
  4.   RectForText: TRect;
  5. begin
  6.   If (Panel = Statusbar1.Panels[1]) and (StatusBar1.Panels[1].Text<>'') then begin
  7.      RectForText := Rect;
  8.      StatusBar1.Canvas.Font.Color:=clRed;
  9.      StatusBar1.Canvas.Brush.Color:=clDefault;
  10.      DrawText(StatusBar.Canvas.Handle, PChar(Panel.Text), -1, RectForText, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
  11.   end
  12.   else
  13.   StatusBar1.Canvas.Font.Color:=clBlack;
  14.  
  15. end;
  16.  
Title: Re: StatusBar panel DrawText special characters
Post by: jcmontherock on September 29, 2022, 11:30:42 am
Which your text and/ or unit encoding ?
Title: Re: StatusBar panel DrawText special characters
Post by: TomTom on September 29, 2022, 11:59:59 am
I don't know if I understand correctly but I think it is UTF8 .
Title: Re: StatusBar panel DrawText special characters
Post by: ASerge on September 29, 2022, 04:46:55 pm
I don't know if I understand correctly but I think it is UTF8 .
This code works great with UTF characters:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
  2.   Panel: TStatusPanel; const Rect: TRect);
  3. const
  4.   CStyle: TTextStyle = (
  5.     Alignment: taLeftJustify;
  6.     Layout: tlCenter;
  7.     SingleLine: True;
  8.     Clipping: True;
  9.     ExpandTabs: False;
  10.     ShowPrefix: False;
  11.     Wordbreak: False;
  12.     Opaque: False;
  13.     SystemFont: False;
  14.     RightToLeft: False;
  15.     EndEllipsis: True);
  16. begin
  17.   if (Panel.Index = 0) and (Panel.Text <> '') then
  18.   begin
  19.     StatusBar.Canvas.Font.Color := clRed;
  20.     StatusBar.Canvas.TextRect(Rect, 0, 0, Panel.Text, CStyle);
  21.   end;
  22. end;
Title: Re: StatusBar panel DrawText special characters
Post by: wp on September 29, 2022, 05:12:47 pm
Do you have the unit "Windows" in the uses clause? In this case, the ANSI version of the DrawText function in the "Windows" unit is used which expects your string to consist of ANSI characters of your system code page --> the output is wrong. On the other hand, if you have the units "LCLIntf" and "LCLType" in the uses clause (with "Windows" removed, or after "Windows"), the call leads into the win32 widgetset routine which converts the original UTF8 string to a widestring and calls the widestring version of DrawText (DrawTextW) --> the output is correct.

Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType
  3.   , Windows              // Comment out Windows, or move it before LCLIntf, and the output will be correct
  4.   ;
  5.  
  6. procedure TForm1.FormPaint(Sender: TObject);
  7. var
  8.   R: TRect;
  9.   s: string;
  10. begin
  11.   R := ClientRect;
  12.   s := '間行連知公教物最掲逮山訪襲駆 Λορεμ ιπσθμ δολορ σιτ αμετ';
  13.   DrawText(Canvas.Handle, PChar(s), Length(s), R, DT_LEFT);
  14. end;
Title: Re: StatusBar panel DrawText special characters
Post by: TomTom on October 03, 2022, 08:50:38 am
@wp That was it. I had to add LCLIntf to uses and it works fine.  Thanks
TinyPortal © 2005-2018