Recent

Author Topic: Drawing Font Face in a TComboBox - [Solved]  (Read 1535 times)

zxandris

  • Full Member
  • ***
  • Posts: 170
Drawing Font Face in a TComboBox - [Solved]
« on: December 17, 2024, 07:58:29 pm »
Now I'm pretty certain I should've been able to find this, I would imagine it's a common question.  But I think my google-fu has failed me.  So I had a play with what I could find and made this.

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGFooterGenerator.cboFontFaceDrawItem(Control: TWinControl;
  2.   Index: Integer; ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.      cboFontFace.canvas.Font.name := cboFontFace.items[index];
  5.      if index = cboFontFace.ItemIndex then
  6.      begin
  7.         cboFontFace.Canvas.Brush.color := clHighlight;
  8.         cboFontFace.Canvas.Font.color := clHighlightText;
  9.         cboFontFace.Canvas.FillRect(ARect);
  10.      end else
  11.      begin
  12.        cboFontFace.Canvas.Brush.color := clWhite;
  13.        cboFontFace.Canvas.Font.color := clBlack;
  14.        cboFontFace.Canvas.FillRect(ARect);
  15.      end;
  16.      cboFontFace.Canvas.TextRect(ARect, 20, ARect.Top, cboFontFace.Items[Index]);
  17. end;

The problem is that when you run your mouse down the various entries it highlights them in turn so you've got a sea of highlighted entries.  Can anyone kindly help me fix this.  I must admit i just don't know why it's doing it.  I would appreciate the help or even if your google-fu is better please point me at an example :)

Thanks

CJ
« Last Edit: December 17, 2024, 08:45:55 pm by zxandris »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1594
    • Lebeau Software
Re: Drawing Font Face in a TComboBox
« Reply #1 on: December 17, 2024, 08:40:47 pm »
You need to look at the State parameter of the event instead of checking the ItemIndex property of the ComboBox, eg:

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGFooterGenerator.cboFontFaceDrawItem(Control: TWinControl;
  2.   Index: Integer; ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   cboFontFace.Canvas.Font.Name := cboFontFace.Items[Index];
  5.   if odSelected in State then // <-- HERE!!!
  6.   // alternatively:
  7.   // if (State * [odSelected,odHotLight]) <> [] then
  8.   begin
  9.     cboFontFace.Canvas.Brush.Color := clHighlight;
  10.     cboFontFace.Canvas.Font.Color := clHighlightText;
  11.   end else
  12.   begin
  13.     cboFontFace.Canvas.Brush.Color := cboFontFace.Color;
  14.     cboFontFace.Canvas.Font.Color := cboFontFace.Font.Color;
  15.   end;
  16.   cboFontFace.Canvas.FillRect(ARect);
  17.   cboFontFace.Canvas.TextRect(ARect, 20, ARect.Top, cboFontFace.Items[Index]);
  18. end;
« Last Edit: December 17, 2024, 08:45:31 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Drawing Font Face in a TComboBox
« Reply #2 on: December 17, 2024, 08:45:42 pm »
Thank you so much, that's sorted that! :)  I figured it was something simple but I honestly have little experience with owner draw.  So thank you a thousand thank yous!

CJ


You need to look at the State parameter of the event instead of checking the ItemIndex property of the ComboBox, eg:

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGFooterGenerator.cboFontFaceDrawItem(Control: TWinControl;
  2.   Index: Integer; ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   cboFontFace.Canvas.Font.Name := cboFontFace.Items[Index];
  5.   if odSelected in State then // <-- HERE!!!
  6.   begin
  7.     cboFontFace.Canvas.Brush.Color := clHighlight;
  8.     cboFontFace.Canvas.Font.Color := clHighlightText;
  9.   end else
  10.   begin
  11.     cboFontFace.Canvas.Brush.Color := cboFontFace.Color;
  12.     cboFontFace.Canvas.Font.Color := cboFontFace.Font.Color;
  13.   end;
  14.   cboFontFace.Canvas.FillRect(ARect);
  15.   cboFontFace.Canvas.TextRect(ARect, 20, ARect.Top, cboFontFace.Items[Index]);
  16. end;

 

TinyPortal © 2005-2018