ListBox with separators: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | The following sample code illustrates how dividing lines (separators) can be inserted between items in order to better group them. The text of items following a separator line must begin with a `'-'` character to define the divider. The listbox `Style` must be set to `lbOwnerDrawVariable` in order to activate owner-drawing with variable line heights: The `OnMeasureItem` event handler is responsible for the line height calculation; it must add a few pixels to the line height of the `'-'` carrying items to reserve space for the separators. And the `OnDrawItem` event handler must paint the separator line and remove the `'-'` from the separator-defining item text. | |
| 2 | 2 | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 3 | + |  | |
| 7 | 4 | ||
| 8 | 5 | ``` pascal | |
| 9 | 6 | unit Unit1; | |
| 10 | 7 | ||
| 11 | 8 | {$mode objfpc}{$H+} | |
| 12 | 9 | ||
| 13 | 10 | interface | |
| 14 | 11 | ||
| 15 | 12 | uses | |
| 16 | 13 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Types; | |
| 17 | 14 | ||
| 18 | 15 | type | |
| 19 | 16 | ||
| 20 | 17 | { TForm1 } | |
| 21 | 18 | ||
| 22 | 19 | TForm1 = class(TForm) | |
| 23 | 20 | ListBox1: TListBox; | |
| 24 | 21 | procedure FormCreate(Sender: TObject); | |
| 25 | 22 | procedure ListBox1DrawItem(Control: TWinControl; Index: Integer; | |
| 26 | 23 | ARect: TRect; State: TOwnerDrawState); | |
| 27 | 24 | procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer; | |
| 28 | 25 | var AHeight: Integer); | |
| 29 | 26 | private | |
| 30 | 27 | ||
| 31 | 28 | public | |
| 32 | 29 | ||
| 33 | 30 | end; | |
| 34 | 31 | ||
| 35 | 32 | var | |
| 36 | 33 | Form1: TForm1; | |
| 37 | 34 | ||
| 38 | 35 | implementation | |
| 39 | 36 | ||
| 40 | 37 | {$R *.lfm} | |
| 41 | 38 | ||
| 42 | 39 | uses | |
| 43 | 40 | LCLType; | |
| 44 | 41 | ||
| 45 | 42 | { TForm1 } | |
| 46 | 43 | ||
| 47 | 44 | procedure TForm1.FormCreate(Sender: TObject); | |
| 48 | 45 | begin | |
| 49 | 46 | with ListBox1.Items do begin | |
| 50 | 47 | Add('Paris'); | |
| 51 | 48 | Add('Rome'); | |
| 52 | 49 | Add('London'); | |
| 53 | 50 | Add('Berlin'); | |
| 54 | 51 | Add('-Casablanca'); | |
| 55 | 52 | Add('Cairo'); | |
| 56 | 53 | Add('Khartoum'); | |
| 57 | 54 | Add('Pretoria'); | |
| 58 | 55 | Add('-Tokyo'); | |
| 59 | 56 | Add('Beijing'); | |
| 60 | 57 | Add('Manila'); | |
| 61 | 58 | Add('-New York'); | |
| 62 | 59 | Add('Chicago'); | |
| 63 | 60 | Add('Rio de Janeiro'); | |
| 64 | 61 | Add('Lima'); | |
| 65 | 62 | Add('-'); | |
| 66 | 63 | end; | |
| 67 | 64 | end; | |
| 68 | 65 | ||
| 69 | 66 | const | |
| 70 | 67 | MARGIN_LINE = 2; | |
| 71 | 68 | MARGIN_TEXT = 12; | |
| 72 | 69 | MARGIN_VERT = 2; | |
| 73 | 70 | HLINE = 1; | |
| 74 | 71 | ||
| 75 | 72 | procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; | |
| 76 | 73 | ARect: TRect; State: TOwnerDrawState); | |
| 77 | 74 | var | |
| 78 | 75 | lb: TListbox; | |
| 79 | 76 | s: String; | |
| 80 | 77 | hasLine: Boolean; | |
| 81 | 78 | dy: Integer; | |
| 82 | 79 | begin | |
| 83 | 80 | lb := Control as TListbox; | |
| 84 | 81 | s := lb.Items[Index]; | |
| 85 | 82 | hasLine := (s <> '') and (s[1] = '-'); | |
| 86 | 83 | if hasLine then dy := HLINE else dy := 0; | |
| 87 | 84 | lb.Canvas.Brush.Style := bsSolid; | |
| 88 | 85 | lb.Canvas.Font.Assign(lb.Font); | |
| 89 | 86 | if odSelected in State then | |
| 90 | 87 | begin | |
| 91 | 88 | if hasLine then | |
| 92 | 89 | begin | |
| 93 | 90 | lb.Canvas.Brush.Color := lb.Color; | |
| 94 | 91 | lb.Canvas.FillRect(ARect.Left, ARect.Top, ARect.Right, ARect.Top+dy); | |
| 95 | 92 | inc(ARect.Top, HLINE + 2); | |
| 96 | 93 | end; | |
| 97 | 94 | if lb.Focused then | |
| 98 | 95 | lb.Canvas.Brush.Color := clHighlight | |
| 99 | 96 | else | |
| 100 | 97 | lb.Canvas.Brush.Color := clGray; | |
| 101 | 98 | lb.Canvas.Font.Color := clHighlightText; | |
| 102 | 99 | end else | |
| 103 | 100 | begin | |
| 104 | 101 | lb.Canvas.Brush.Color := lb.Color; | |
| 105 | 102 | lb.Canvas.Font.Color := clWindowText; | |
| 106 | 103 | dy := 0; | |
| 107 | 104 | end; | |
| 108 | 105 | ||
| 109 | 106 | lb.Canvas.FillRect(ARect); | |
| 110 | 107 | ||
| 111 | 108 | if hasLine then | |
| 112 | 109 | begin | |
| 113 | 110 | if not (odSelected in State) then | |
| 114 | 111 | begin | |
| 115 | 112 | lb.Canvas.Pen.Style := psSolid; | |
| 116 | 113 | lb.Canvas.Pen.Color := clWindowText; | |
| 117 | 114 | lb.Canvas.Line( | |
| 118 | 115 | ARect.Left + MARGIN_LINE, | |
| 119 | 116 | ARect.Top + HLINE, | |
| 120 | 117 | ARect.Right - MARGIN_LINE, | |
| 121 | 118 | ARect.Top + HLINE | |
| 122 | 119 | ); | |
| 123 | 120 | end; | |
| 124 | 121 | Delete(s, 1, 1); | |
| 125 | 122 | end; | |
| 126 | 123 | ||
| 127 | 124 | lb.Canvas.Brush.Style := bsClear; | |
| 128 | 125 | lb.Canvas.TextOut( | |
| 129 | 126 | ARect.Left + MARGIN_TEXT, | |
| 130 | 127 | (ARect.Top + dy + ARect.Bottom - lb.Canvas.TextHeight('Tg')) div 2, | |
| 131 | 128 | s | |
| 132 | 129 | ); | |
| 133 | 130 | end; | |
| 134 | 131 | ||
| 135 | 132 | procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer; | |
| 136 | 133 | var AHeight: Integer); | |
| 137 | 134 | var | |
| 138 | 135 | lb: TListbox; | |
| 139 | 136 | h: Integer; | |
| 140 | 137 | s: String; | |
| 141 | 138 | begin | |
| 142 | 139 | lb := Control as TListBox; | |
| 143 | 140 | lb.Canvas.Font.Assign(lb.Font); | |
| 144 | 141 | h := lb.Canvas.TextHeight('Tg') + MARGIN_VERT * 2; | |
| 145 | 142 | s := lb.Items[Index]; | |
| 146 | 143 | if (s <> '') and (s[1] = '-') then | |
| 147 | 144 | inc(h, HLine); | |
| 148 | 145 | AHeight := h; | |
| 149 | 146 | end; | |
| 150 | 147 | ||
| 151 | 148 | end. | |
| 152 | 149 | ``` | |
| 153 | 150 | ||
| 154 | 151 | --- | |
| 155 | 152 | *From the Free Pascal wiki, page "ListBox with separators", as it stood on 2023-08-19 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 156 | 153 | Category: Code Snippets |
