Recent

Author Topic: red dot in the listbox  (Read 4060 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
red dot in the listbox
« on: May 24, 2016, 12:21:48 am »
Who tells me how to draw a colored dot at the beginning of each row of a listbox? Just as well a square. Just that I can put the color at the beginning of the item. Accepted cross-platform solutions
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: red dot in the listbox
« Reply #2 on: May 24, 2016, 02:41:04 am »
Basic example:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. var
  4.   LB: TListBox;
  5.   tmpStyle: TTextStyle;
  6.   BRect: TRect;
  7.   VCenter: LongInt;
  8. begin
  9.   if not (Control is TListBox) then
  10.     exit;
  11.   LB := TListBox(Control);
  12.   with LB.Canvas do
  13.   begin
  14.     { Fill the background }
  15.     if (odSelected in State) then
  16.       Brush.Color := clLime
  17.     else
  18.       Brush.Color := LB.Color;
  19.     FillRect(ARect);
  20.  
  21.     { Draw a red rectangle }
  22.     VCenter := (ARect.Bottom+ARect.Top) div 2;
  23.     BRect := Bounds(ARect.Left+2, VCenter-4, 4, 4);
  24.     Brush.Color := clRed;
  25.     FillRect(BRect);
  26.     { Draw a blue rectangle }
  27.     BRect := Bounds(ARect.Left+6, VCenter, 4, 4);
  28.     Brush.Color := clBlue;
  29.     FillRect(BRect);
  30.  
  31.     { Draw the text }
  32.     Font.Color := clNavy; { or clWindowText }
  33.     tmpStyle := TextStyle;
  34.     tmpStyle.Layout := tlCenter; { Center the text vertically }
  35.     tmpStyle.Opaque := False;  { Leave the background intact }
  36.     TextRect(ARect, ARect.Left+2+4+4+2, ARect.Top, LB.Items[Index], tmpStyle);
  37.   end;
  38. end;
  39.  

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: red dot in the listbox
« Reply #3 on: May 24, 2016, 08:49:59 am »
It does not work on mac
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

balazsszekely

  • Guest
Re: red dot in the listbox
« Reply #4 on: May 24, 2016, 08:54:12 am »
@xinyiman
It does! I can make a screenshot if you like.  :)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: red dot in the listbox
« Reply #5 on: May 24, 2016, 09:40:39 am »
attach source . because one does not do me
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

balazsszekely

  • Guest
Re: red dot in the listbox
« Reply #6 on: May 24, 2016, 09:49:49 am »
Quote
@xinyiman
attach source . because one does not do me
What source? @engkin already provided the source. Add lcltype to the uses clauses and set listbox style property to lbOwnerDrawFixed.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: red dot in the listbox
« Reply #7 on: May 24, 2016, 09:55:52 am »
you're right, I had forgotten to set the property .

lbOwnerDrawFixed

I am just stupid one. Thank you all
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: red dot in the listbox
« Reply #8 on: May 24, 2016, 10:18:20 am »
a curiosity , if I wanted to do the same thing on a combobox I have to make some changes and in theory I just copy and paste the code into the combobox making sure to change TListBox with TComboBox
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

balazsszekely

  • Guest
Re: red dot in the listbox
« Reply #9 on: May 24, 2016, 11:13:56 am »
Quote
@xinyiman
a curiosity , if I wanted to do the same thing on a combobox I have to make some changes and in theory I just copy and paste the code into the combobox making sure to change TListBox with TComboBox
Under OSX the TComboBox OnDrawItems event is never called, so it won't work: http://forum.lazarus.freepascal.org/index.php/topic,32104.msg206679.html#msg206679

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: red dot in the listbox
« Reply #10 on: May 24, 2016, 11:40:23 am »
Ok, thank you  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018