[SOLVED] TCheckListBox — drawing items with CheckBoxes lazarus / 31-LCL ---------------------------------------------------------------------- flowCRANE · 2019-07-09 00:25 I have a standard TCheckListBox filled with data, and I need to use custom drawing event, because the items must be taller than the default and looks different. But if I set the ItemHeight property to the needed height (for example 32) and set the Style to the lbOwnerDrawFixed, checkboxes are not painted by default, even if I don't generate an OnDrawItem event (see the attachment). So, how can I draw items in an OnDrawItem event with checkboxes? These checkboxes must look like system controls — I don't want to use custom graphics. Thank you in advance for eny tips. ---------------------------------------------------------------------- jamie · 2019-07-09 01:35 you need to draw your own check box/Mark. You also need to move your text over to make room for it..  The event gives you the state of the control so you can make your choice of images or marks  If you want to do Unicode you can because it does have a check box in the list but personally I would use a bitmap image.. ---------------------------------------------------------------------- flowCRANE · 2019-07-09 01:46 Quote from: jamie on July 09, 2019, 03:35:33 amyou need to draw your own check box/Mark. So it sucks... QuoteYou also need to move your text over to make room for it.. Of course, but I don't know how much space I should leave because GetCheckWidth method is protected and I can't use it. I tried to use a code similar to that of this method, but I could not write the correct solution. QuoteIf you want to do Unicode you can because it does have a check box in the list but personally I would use a bitmap image.. Yes, but I still don't know how much space is needed for checkboxes. But I have an idea — I can use ThemeServices. ---------------------------------------------------------------------- jamie · 2019-07-09 02:10 why not use a Timagelist and pick out an image from that. Also I think TimageList also has a way to scale the drawing automatically to your DPI.., I would need to look into that, but in any case you can stretchDraw the image..  A TRect is given to you on the drawing cycle so you should already know the work space you have.. ---------------------------------------------------------------------- flowCRANE · 2019-07-09 02:20 Quote from: jamie on July 09, 2019, 04:10:37 amwhy not use a Timagelist and pick out an image from that. Because I want a native look — native checkboxes, native colors of the font and background, but text rendered in a different way (some bold, some multiline, some with additional graphics etc.). ---------------------------------------------------------------------- ASerge · 2019-07-09 13:20 Quote from: furious programming on July 09, 2019, 04:20:46 amBecause I want a native look — native checkboxes, native colors of the font and background, but text rendered in a different way (some bold, some multiline, some with additional graphics etc.). Rendering is implemented in the descendants of TWSCustomCheckListBox, depending on the platform. For example for Windows in Win32WSCheckLst.pp, see DefaultWndHandler.DrawCheckListBoxItem. And Yes, it will have to work hard. ---------------------------------------------------------------------- flowCRANE · 2019-07-09 18:40 Thanks, @ASerge. I tried to use the code of the GetCheckWidth inside an OnDrawItem event in this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if HandleAllocated then  CheckBoxWidth := TWSCustomCheckListBoxClass(MyLi stBox.WidgetSetClass).GetCheckWidth(MyListBox)else  CheckBoxWidth := 0; but I got SIGSEGV. But this is not important — I can use ThemeServices manually and do everything by myself, just like in the DefaultWndHandler method. It is only a few lines, I can do it. ---------------------------------------------------------------------- wp · 2019-07-09 19:08 The best code library is contained in Lazarus itself. Look at TCustomGrid (unit Grids), method TCustomGrid.DrawGridCheckBoxBitmaps, in the else branch wherever you see ThemeServices. Or TAChart's TChartListbox (unit TAChartListBox). It is a customdrawn listbox with checkboxes: DrawItem, MeasureItem, CalcRects. ---------------------------------------------------------------------- flowCRANE · 2019-07-09 19:50 I made it — I ported the code from the DefaultWndHandler, but I used a high-level classes/types to set canvas properties, draw text and so on. The working solution is below (visual result in the attachment): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TScoresListForm.CScoresListBoxDrawItem(AControl: TWinControl; AIndex: Integer; ARect: TRect; AState: TOwnerDrawState);const  CHECKBOX_STATE: array [TCheckBoxState, Boolean] of TThemedButton = (    (tbCheckBoxUncheckedDisabled, tbCheckBoxUncheckedNormal),    (tbCheckBoxCheckedDisabled, tbCheckBoxCheckedNormal),    (tbCheckBoxMixedDisabled, tbCheckBoxMixedNormal)  );var  ListBox: TCheckListBox absolute AControl;var  CheckBoxDetails: TThemedElementDetails;  CheckBoxSize: TSize;  CheckBoxEnabled: Boolean;var  CheckBoxRect, TextRect: TRect;  TextStyle: TTextStyle;begin  // item enabled state  CheckBoxEnabled := ListBox.Enabled and ListBox.ItemEnabled[AIndex];   // listbox background (usually white)  ListBox.Canvas.Brush.Color := Windows.GetSysColor(COLOR_WINDOW);  ListBox.Canvas.FillRect(ARect);   // checkbox details  CheckBoxDetails := ThemeServices.GetElementDetails(CHECKBOX_STATE[ListBox.State[AIndex], CheckBoxEnabled]);  CheckBoxSize := ThemeServices.GetDetailSize(CheckBoxDetails);  CheckBoxRect := Bounds(ARect.Left + 4, ARect.Top + (ARect.Height - CheckBoxSize.Height) div 2, CheckBoxSize.Width, CheckBoxSize.Height);   // checkbox drawing  ThemeServices.DrawElement(ListBox.Canvas.Handle, CheckBoxDetails, CheckBoxRect);   // text area  TextRect := ARect;  TextRect.Left := CheckBoxRect.Right + 4;   // text background (in my system blue)  if odSelected in AState then  begin    ListBox.Canvas.Brush.Color := Windows.GetSysColor(COLOR_HIGHLIGHT);    ListBox.Canvas.FillRect(TextRect);  end;   // text properties  TextStyle := ListBox.Canvas.TextStyle;  TextStyle.Layout := tlCenter;   // first item is always bolded  if AIndex = 0 then    ListBox.Canvas.Font.Style := [fsBold];   // text drawing  ListBox.Canvas.Brush.Style := bsClear;  ListBox.Canvas.TextRect(TextRect, TextRect.Left + 4, TextRect.Top, ListBox.Items[AIndex], TextStyle);   // focus rect drawing  if odFocused in AState then  begin    ListBox.Canvas.DrawFocusRect(ARect); // this covers the default focus rect, wide as whole item    ListBox.Canvas.DrawFocusRect(TextRect); // this draws a correct focus rect, only around the text  end;end; Thank you all for help — problem solved. ---------------------------------------------------------------------- ASerge · 2019-07-10 21:37 Quote from: furious programming on July 09, 2019, 09:50:20 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...  // listbox background (usually white)  ListBox.Canvas.Brush.Color := Windows.GetSysColor(COLOR_WINDOW);  ListBox.Canvas.FillRect(ARect);...Can be no "usually"?Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...  ListBox.Canvas.Brush.Color := ListBox.GetColorResolvingParent;  ListBox.Canvas.FillRect(ARect);... ---------------------------------------------------------------------- wp · 2019-07-10 22:45 Quote from: ASerge on July 10, 2019, 11:37:10 pm  ListBox.Canvas.Brush.Color := ListBox.GetColorResolvingParent; Wow, never seen this one! I would have used "clWindow" instead of "Windows.GetSysColor(COLOR_WINDOW)". ---------------------------------------------------------------------- ASerge · 2019-07-10 22:59 Quote from: wp on July 11, 2019, 12:45:54 amQuote from: ASerge on July 10, 2019, 11:37:10 pm  ListBox.Canvas.Brush.Color := ListBox.GetColorResolvingParent; Wow, never seen this one! See description in https://lazarus- ccr.sourceforge.io/docs/lcl/controls/tcontrol.color.html ---------------------------------------------------------------------- flowCRANE · 2019-07-12 19:46 Quote from: ASerge on July 10, 2019, 11:37:10 pmCan be no "usually"? In the default system theme, controls like Edit, ComboBox, ListBox etc. has white background. But there is a possibility to set the dark theme (high contrast or something like this) and the background will be dark (or even black). My application must look like other system windows, so I have to get the system color. Your proposition works the same — background color of the items/control is proper. But more logic is needed, and finally the clWindow color will be used anyway. So, in total, I can use the clWindow color, instead of using the GetSysColor function. ---------------------------------------------------------------------- jamie · 2019-07-12 21:22 You are better off using the clWindow because it performs much better, no need to call functions plus it should be cross platform this way.. ---------------------------------------------------------------------- flowCRANE · 2019-07-13 01:54 Cross-platform solution is not needed — this application is only for Windows systems. But you're right, it's better to use a color constant than to call a function. ---------------------------------------------------------------------- ASerge · 2019-07-13 06:45 Quote from: furious programming on July 13, 2019, 03:54:03 amBut you're right, it's better to use a color constant than to call a function. Until you want to change the color of the listbox or set ParentColor ---------------------------------------------------------------------- flowCRANE · 2019-07-14 22:32 No problem — this will never happen.