TIP: Click on subject to list as thread! ANSI
echo: delphi
to: SIMON HUGGINS
from: MARTIN MAAT
date: 1997-05-18 09:58:00
subject: Owner draw listboxes

Hallo Simon
 MM> Stop this immediately and check out the DrawTextEx API
 SH> I'm using Delphi 1 (for compatibility reasons) at the moment but I
 SH> found there's one called DrawText in the 3.1 API.
 SH> I still can't get this to work though. The height it returns is 0
 SH> :( 
 SH> procedure Tdiaryfrm.left_listMeasureItem (Control: TWinControl;  
 SH>                      Index: Integer; var Height: Integer);
 SH> var
 SH>    temp :string;
 SH>    t    :pchar ;
 SH>    r    :Trect ;
 SH> begin
 SH>   temp:=(control as Tlistbox).items[index];
 SH>   t:=stralloc(length(temp)+2);
 SH>   strpcopy(t,temp);
 SH>   with (control as Tlistbox).canvas do
 SH>           r:=rect(left,0,left+width,height);
 SH>   Height:=drawtext( hwnd(control), t, -1, r ,
 SH>      DT_WORDBREAK+DT_CALCRECT); strdispose(t);
 SH> end;
I'm not sure if it makes any difference in this case, but you
should use Control.Canvas.Handle instead of hwnd(control). The
canvas' handle is not the same as the control's handle.
 SH> procedure Tdiaryfrm.left_listDrawItem(Control: TWinControl; Index:
 SH>             Integer;   Rect: TRect; State: TOwnerDrawState);
 SH> var
 SH>   temp :string;
 SH>    t   :pchar ;
 SH> begin
 SH>   temp:=(control as Tlistbox).items[index];
 SH>   t:=stralloc(length(temp)+2);
 SH>   strpcopy(t,temp);
 SH>   drawtext( hwnd(control), t, length(temp), Rect, DT_WORDBREAK);
 SH>   strdispose(t);
 SH> end;
Same here. QUAD-Tip: add a chr(0) to the pascal style string and feed
PChar(@temp) + 1 to DrawText instead of t. There is no need to create a 
PChar, allocate memory and deallocate it again. I know, it's untidy, and you 
should change it to PChar(temp) once you transfer to Delphi 2 or 3. You know 
what, forget it al together :-).
 SH> Could it be that my rectangle is completely wrong?
 SH> I don't know how to find the rectangle in the MeasureItem bit. 
You're not changing Rect in DrawItem, so that should be the Rect you want. In 
MeasureItem, rect's height is irrelevant. Rect's width is important though, 
since height is calculated wrapping text around. You initialise your rect 
like this:
 with (control as Tlistbox).canvas do
         r:=rect(left,0,left+width,height);
which could be a lot shorter:
  r := Control.ClientRect;
but should be OK either way.
Groeten, Martin.
--- FMail/386 1.02
---------------
* Origin: -=[ E-mail: m_maat@knoware.nl .......... ]=- (2:286/420.1)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.