Can I programmatically set the position of ComboBox dropdown list?

前端 未结 2 1930
故里飘歌
故里飘歌 2020-12-21 00:41

Ordinary Windows ComboBox (csDropDown or csDropDownList style) will open its dropdown list right below or, if no space left below, above the combo.

2条回答
  •  清歌不尽
    2020-12-21 01:18

    Well, you can do this by using GetComboBoxInfo to obtain a handle to the window used for the list, and then move that window. Like this:

    type
      TMyForm = class(TForm)
        ComboBox1: TComboBox;
        procedure ComboBox1DropDown(Sender: TObject);
      protected
        procedure WMMoveListWindow(var Message: TMessage); message WM_MOVELISTWINDOW;
      end;
    
    ....
    
    procedure TMyForm.ComboBox1DropDown(Sender: TObject);
    begin
      PostMessage(Handle, WM_MOVELISTWINDOW, 0, 0);
    end;
    
    procedure TMyForm.WMMoveListWindow(var Message: TMessage);
    var
      cbi: TComboBoxInfo;
      Rect: TRect;
      NewTop: Integer;
    begin
      cbi.cbSize := SizeOf(cbi);
      GetComboBoxInfo(ComboBox1.Handle, cbi);
      GetWindowRect(cbi.hwndList, Rect);
      NewTop := ClientToScreen(Point(0, ComboBox1.Top-Rect.Height)).Y;
      MoveWindow(cbi.hwndList, Rect.Left, NewTop, Rect.Width, Rect.Height, True);
    end;
    

    I have ignored the issue of error checking to keep the code simple.

    However, be warned that it looks pretty horrible because the dropdown animation is still shown. Perhaps you can find a way to disable that.

    However, you simply do not need to do anything like this because Windows already does it for you. Drag a form to the bottom of the screen and drop down your combo. Then you will see the list appear above the combo. Like this:

    enter image description here

提交回复
热议问题