Larry Lustig
Larry Lustig

Reputation: 50970

Match TButtonedEdit image to TComboBox default image

My application uses standard TComboBoxes and also TButtonedEdits to produce controls with more complex drop-down panels. I would like the two controls to look the same. In particular, I would like image on my TButtonedEdits to be identical to the image on the TComboBoxes regardless of which current or future operating system the program is run on (that is, assuming that this image is determined by the operating system and not be Delphi).

I assume that I will have to install, at runtime, the resource providing the image to TComboBox into a TImageList to make it available to my TButtonedEdits. How do I locate and extract that resource?

Upvotes: 1

Views: 764

Answers (1)

Uli Gerhardt
Uli Gerhardt

Reputation: 14001

You can use the theme engine to draw the button yourself - try something like this for starters:

uses
  Themes;

procedure DrawComboBoxButton(ACanvas: TCanvas; ADown, AMouseInControl: Boolean; const ARect: TRect);
var
  ComboElem: TThemedComboBox;
  Details: TThemedElementDetails;
begin
  if ThemeServices.ThemesEnabled then
  begin
    if ADown then
      ComboElem := tcDropDownButtonPressed
    else if AMouseInControl then
      ComboElem := tcDropDownButtonHot
    else
      ComboElem := tcDropDownButtonNormal;
    Details := ThemeServices.GetElementDetails(ComboElem);
    ThemeServices.DrawElement(ACanvas.Handle, Details, ARect);
  end
  else
  begin
    if ADown then
      DrawFrameControl(ACanvas.Handle, ARect, DFC_SCROLL, DFCS_SCROLLCOMBOBOX or DFCS_PUSHED)
    else
      DrawFrameControl(ACanvas.Handle, ARect, DFC_SCROLL, DFCS_SCROLLCOMBOBOX);
  end;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  DrawComboBoxButton(PaintBox1.Canvas, False, False, Bounds(0, 0, 20, 20));
  DrawComboBoxButton(PaintBox1.Canvas, True, False, Bounds(20, 0, 20, 20));
end;

(adapted from the thread "Windows themes in combobox" in the Embarcadero forums).

Mike Lischke's "Windows XP Theme Explorer" can help you to find the right "Elements" and "Details". And have a look at this SO thread.

Upvotes: 2

Related Questions