Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting an image inside an combobox (right-hand edge) in Delphi/Win32

I want draw a an image inside an combobox (right-hand edge) in Delphi/Win32.

The combobox has the style csDropDown. This doesn't work with csOwnerDrawFixed or csOwnerDrawVariable.

The combobox should be editable similar to the address bar of the browser.

Is there a Win32 solution without creating an additional Delphi component?

I tried the following, but it doesn't work. Can I do that with Delphi 7?

TForm1 = class(TForm)
  ...
private
  FChDirComboWndProc: TWndMethod;
  procedure ChDirComboWndProc(var Message: TMessage);
  ...

procedure TForm1.FormCreate(Sender: TObject);
begin
  FChDirComboWndProc := ChDirComboBox.WindowProc; // save old window proc
  ChDirComboBox.WindowProc := ChDirComboWndProc; // subclass
end;

procedure TForm1.ChDirComboWndProc(var Message: TMessage);
begin
    WM_ERASEBKGND: begin    // WM_PAINT ?
        SetBkMode(Message.WParam, TRANSPARENT);
        SetTextColor(Message.wParam, GetSysColor(COLOR_GRAYTEXT));
        FillRect(Message.wParam, Rect(3,3,300,30), GetStockObject(BLACK_BRUSH ));
        Rectangle(Message.wParam, 15,15, 100, 100); //Test
        OutputDebugString(PCHar(Format('aa %d %d %d',[Message.WParam, Message.LParam, ChDirComboBox.Handle])));
      end;
  end;
  FChDirComboWndProc(Message); // process message
end;
like image 790
Codr Avatar asked Oct 13 '11 09:10

Codr


1 Answers

The way to do it is to implement an Owner-Drawn Combo Boxes. See Owner-Drawn Combo Boxes on MSDN, or look for Delphi sample, e.g. Owner Draw - ComboBox.

like image 163
Roman R. Avatar answered Nov 15 '22 05:11

Roman R.