Recent

Author Topic: [SOLVED] Draw Alternate Lines Gray in TListBox  (Read 1160 times)

zxandris

  • Full Member
  • ***
  • Posts: 163
[SOLVED] Draw Alternate Lines Gray in TListBox
« on: July 06, 2024, 12:03:40 pm »
I did search how to do this, must be using the wrong search term because oddly didn't find a way.

I want to draw the alternate lines Grey and the other lines White and account for highlighted line.  I have the following

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGOptions.lbDictDrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. var
  4.   ListBox: TListBox;
  5.   Canvs: TCanvas;
  6. begin
  7.   ListBox := Control as TListBox;
  8.   Canvs := ListBox.Canvas;
  9.   if Index mod 2 = 0 then
  10.   begin
  11.        Canvs.Brush.Color := clSilver;
  12.        Canvs.FillRect(ARect);
  13.        canvs.Pen.color := clWindowText;
  14.   end else
  15.   begin
  16.        Canvs.Brush.Color := clWindow;
  17.        Canvs.FillRect(ARect);
  18.        canvs.Pen.color := clWindowText;
  19.   end;
  20.   if ListBox.ItemIndex = Index then
  21.   begin
  22.        Canvs.Brush.Color := clHighlight;
  23.        canvs.FillRect(ARect);
  24.        canvs.Pen.color := clHighlightText;
  25.   end;
  26.   Canvs.TextOut(ARect.Left, ARect.Top, ListBox.Items[Index]);
  27. end;
  28.  

It works - ish, when selecting a line it highlights it okay, then for some reason on the 'white' lines it will then leave the background as the highlight color and not clWindowText.

Any help would be gratefully appreciated, I just don't see why it's not working!  As I said I did do a search, my google-fu must not be that strong.

CJ
« Last Edit: July 06, 2024, 08:11:05 pm by zxandris »

Nicole

  • Hero Member
  • *****
  • Posts: 1271
Re: Draw Alternate Lines Gray in TListBox
« Reply #1 on: July 06, 2024, 12:39:42 pm »
you can try a
.....refresh;

for some of your items. Give it a try, which one makes sense.

zxandris

  • Full Member
  • ***
  • Posts: 163
Re: Draw Alternate Lines Gray in TListBox
« Reply #2 on: July 06, 2024, 08:10:53 pm »
Okay, thanks for the replies guys, but I actually did some experimentation and the below code seems to work for what I wanted. (It also draws a little graphic but that was extra).  But it colors alternate lines an accounts for selected items (even multiple).

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGOptions.lbDictDrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. var
  4.    ListBox: TListBox;
  5.    Canvs: TCanvas;
  6.    bmp  : TBitmap;
  7. begin
  8.       ListBox := Control as TListBox;
  9.       Canvs := ListBox.Canvas;
  10.       if ListBox.selected[Index] then
  11.       begin
  12.            Canvs.Brush.Color := clHighlight;
  13.            canvs.Pen.color := clHighlightText;
  14.            Canvs.Font.color := clHighlightText;
  15.            canvs.FillRect(ARect);
  16.       end else
  17.       begin
  18.            if Index mod 2 = 0 then
  19.            begin
  20.                 Canvs.Brush.Color := clGray;
  21.                 canvs.Pen.color := clWhite;
  22.                 canvs.Font.Color := clWhite;
  23.                 Canvs.FillRect(ARect);
  24.            end else
  25.            begin
  26.                 Canvs.Brush.Color := clWindow;
  27.                 canvs.Pen.color := clWindowText;
  28.                 canvs.Font.Color := clWindowText;
  29.                 Canvs.FillRect(ARect);
  30.            end;
  31.       end;
  32.       bmp := TBitmap.create;
  33.       try
  34.          ilDict_16px.GetBitmap(0, bmp);
  35.          Canvs.Draw(ARect.Left, ARect.Top, bmp);
  36.       finally
  37.              bmp.free;
  38.       end;
  39.       Canvs.TextOut(ARect.Left + 18, ARect.Top, ListBox.Items[Index]);
  40. end;
  41.  

Now that works for me, but you have to set the listbox to owner draw fixed and there is no resizing on my form as it's an options dialog anyway.

I put this here in the hopes it helps other peeps.

CJ

Bart

  • Hero Member
  • *****
  • Posts: 5612
    • Bart en Mariska's Webstek
Re: [SOLVED] Draw Alternate Lines Gray in TListBox
« Reply #3 on: July 06, 2024, 10:51:45 pm »
The proper way is to check for (odSeleted in State) to examine wether an item is selected or not.

Bart

 

TinyPortal © 2005-2018