Recent

Author Topic: Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw  (Read 460 times)

jamie

  • Hero Member
  • *****
  • Posts: 7516
Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw
« on: January 15, 2026, 11:17:12 pm »

It would be nice if the lBVirtual style included the OwnerDraw and maybe the OwnerDrawVariable style attached to the lbVirtual.

Just saying..

Jamie
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw
« Reply #1 on: January 16, 2026, 12:03:15 pm »
lbVirtual is OwnerDrawFixed by design (see implementation deatils and the MS Docs).
This may very well be not exactly Delphi compatible, but it is what it it is.
I'm not even going to try to make it OwnerDrawVariable.

Bart

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw
« Reply #2 on: January 16, 2026, 02:08:56 pm »
I just tested it: Set ListBox.Style to lbVirtual, ListBox.Count to some value, and provided a handler for OnData. That's all to set virtual mode.

Then I switched ItemHeight to a new value --> the height of the items in the listview changes accordingly.

I added an empty OnDrawItem handler --> the items are gone, there is a uniform gray background. I completed the OnDrawItem with
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   if Odd(Index) then
  5.   begin
  6.     Listbox1.Canvas.Font.Style := [fsBold];
  7.     Listbox1.Canvas.Brush.Color := clMoneyGreen;
  8.   end else
  9.   begin
  10.     Listbox1.Canvas.Font.Style := [fsItalic];
  11.     Listbox1.Canvas.Brush.Color := clWindow;
  12.   end;
  13.   if ([odSelected, odFocused] * State <> []) then
  14.   begin
  15.     Listbox1.Canvas.Brush.Color := clHighlight;
  16.     Listbox1.Canvas.Font.color := clHighlightText;
  17.   end;
  18.   Listbox1.Canvas.FillRect(ARect);
  19.   Listbox1.Canvas.TextOut(ARect.Left+ 2, ARect.Top+2, Listbox1.Items[Index]);
  20. end;
and now I can see alternating rows of bold/moneygreen and italic/white.

Conclusions: As Bart said, the style lbVirtual is able to customdraw the listbox items in virtual mode provided that an OnDrawItem handler is available. If not the default drawing code is used.

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw
« Reply #3 on: January 16, 2026, 06:20:19 pm »
@wp: maybe add the example to our wiki?

Bart

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: Newly revised TlistBox in Trunk forgot the lbVirtualOwnerDraw
« Reply #4 on: January 16, 2026, 06:23:04 pm »
« Last Edit: January 16, 2026, 11:24:04 pm by wp »

 

TinyPortal © 2005-2018