Recent

Author Topic: TCheckListBox custom OnDrawItem when form disabled  (Read 1422 times)

andyH

  • Jr. Member
  • **
  • Posts: 99
TCheckListBox custom OnDrawItem when form disabled
« on: April 25, 2021, 12:53:34 am »
Note sure whether this should be in the linux or LCL area?

I have a TCheckListBox with a custom OnDrawItem, relevant definition for the TCheckListBox:
Code: Pascal  [Select][+][-]
  1. Box2.Style:= lbOwnerDrawFixed;
  2. Box2.OnDrawItem:= @BoldOS;
With BoldOS as
Code: Pascal  [Select][+][-]
  1. procedure TBasePanel.BoldOS(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
  2. const
  3.   AdjustL = 5;
  4.   AdjustT = 3;
  5. begin
  6.   with (Control as TCheckListBox) do
  7.        begin
  8.          if MyDrives.isOS(Items[Index]) then Canvas.Font.Bold:= true;
  9.          Canvas.TextOut(ARect.Left + AdjustL,ARect.Top + AdjustT,Items[Index]);
  10.        end;
  11. end;
When an operating system is detected on a partition it displays the text in bold. It works. But...
  • Enabled.png is the mainform enabled, display as wanted.
  • Disabled.png is the mainform disabled (which I need to do when child forms are displayed).
When the main form is disabled I want Box2 to look the same as Box1 (which has no custom OnDrawItem defined - uses the defaults), i.e. grey background with greyed text and I've not been able to find out how to do it.

First issue was detecting that the main form has been disabled, if not MainFm.Enabled seems clumsy given that it is automatically detected in Box1. Then there is what colours to use, given that I don't want to use specific colours, I want the theme defaults?

andyH

  • Jr. Member
  • **
  • Posts: 99
Re: TCheckListBox custom OnDrawItem when form disabled
« Reply #1 on: April 26, 2021, 11:25:04 pm »
Well I got there, but it was like pulling teeth. Along the way I found out that:
  • While disabling the main form disables all the embedded objects it does not set enabled:= false on them, it has to be done explicitly.
  • Canvas.TextOut redraws the background while using Canvas.TextRect with the property Opaque set false does not.
  • Canvas.TextRect ignores Canvas.Font.Bold:= true.
  • On a disabled TCheckListBox/ListBox using Canvas.TextRect with the font colour clGrayText sets the font colour correctly, but a default disabled TCheckListBox/ListBox appears to have a white outline around the text. This was achieved by using TextRect twice, once with white text shifted one pixel down and right, and then again with clGrayText text not shifted.

The resultant code:
Code: Pascal  [Select][+][-]
  1. procedure TBasePanel.BoldOS(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
  2. const
  3.   AdjustL = 5;
  4.   AdjustT = 3;
  5. var
  6.   BoxStyle : TTextStyle;
  7. begin
  8.   with (Control as TCheckListBox) do
  9.        if enabled then
  10.           begin
  11.             if MyDrives.isOS(Items[Index]) then Canvas.Font.Bold:= true;
  12.             Canvas.TextOut(ARect.Left + AdjustL,ARect.Top + AdjustT,Items[Index]);
  13.           end
  14.        else
  15.           begin
  16.             BoxStyle.Opaque:= false;
  17.             BoxStyle.Layout:= tlTop;
  18.             Canvas.Font.color:= clWhite;
  19.             Canvas.TextRect(ARect, ARect.Left+AdjustL+1,ARect.Top+AdjustT+1,Items[Index], BoxStyle);
  20.             Canvas.Font.color:= clGrayText;
  21.             Canvas.TextRect(ARect, ARect.Left+AdjustL,ARect.Top+AdjustT,Items[Index], BoxStyle);
  22.           end;
  23. end; //end procedure TBasePanel.BoldOS
The end effect as the screenshot where the TCheckListBox on the right has the custom OnDrawItem.

If there is a better way of doing this I'd like to know  ::)

 

TinyPortal © 2005-2018