Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

How to recalculate line heights of ownerdrawn listboxes

Title: How to recalculate line heights of ownerdrawn listboxes Question: Listboxes with the ownerdrawvariable style calculate the item height only when a new item is added. How can item heights be recalculated subsequently? Answer: Listboxes are among the simplest Windows controls, but their appearance can be adjusted to a high degree by taking advantage of the OwnerDraw and OwnerDrawVariable styles. In case of the OwnerDrawVariable style, it may be an annoyance from time to time, however, that the Listbox seems to calculate line height only when items are added. This behavior is different from statements in the Delphi online help saying that the OnMeasureItem event "occurs when the application needs to redisplay an item in a variable height owner-draw list box" (D7). The following procedure forces recalculation of the listbox item heights. It calls the OnMeasureItem event handler for each line to determine the requested line heights, and then passes the results to the listbox by means of the Windows message LB_SETITEMHEIGHT. procedure Listbox_CalcLineHeights(AListbox:TListbox); var i, h : integer; MeasureItemEvent : TMeasureItemEvent; begin if Assigned(AListbox) and AListbox.HandleAllocated then begin with AListbox do begin MeasureItemEvent := OnMeasureItem; if Assigned(MeasureItemEvent) then begin for i:=0 to Items.Count-1 do begin MeasureItemEvent(AListbox, i, h); SendMessage(Handle, LB_SETITEMHEIGHT, i, h); end; end; end; end; end;