如何检查组合框下拉列表显示的是向上还是向下?

jot*_*bek 7 .net c# combobox drop-down-menu

我有从combobox继承的控件(已实现C#、. Net 2.0)。它具有过滤和其他内容。为了保持UI的正确性,当过滤期间的项目数下降时,下拉列表会更改其大小以适合剩余的项目数(这是通过NativeMethods.SetWindowPos(...)完成的)。

有什么方法可以检查下拉列表是向上显示还是向下显示(不是字面上显示的)-不是检查列表是否打开,而是打开,而是向上还是向下?

干杯,jbk

And*_*aus 6

ComboBox有两个事件(DropDownDropDownClosed),当下拉部分打开和关闭时会触发这些事件,因此您可能希望将处理程序附加到它们以监视控件的状态。

另外,还有一个布尔属性(DroppedDown)应该告诉您当前状态。


jot*_*bek 3

所以我找到了一个答案:

这里我们有组合框的两个句柄:

    /// <summary>
    /// Gets a handle to the combobox
    /// </summary>
    private IntPtr HwndCombo
    {
        get
        {
            COMBOBOXINFO pcbi = new COMBOBOXINFO();
            pcbi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pcbi);
            NativeMethods.GetComboBoxInfo(this.Handle, ref pcbi);
            return pcbi.hwndCombo;
        }
    }
Run Code Online (Sandbox Code Playgroud)

以及组合框的下拉列表:

    /// <summary>
    /// Gets a handle to the combo's drop-down list
    /// </summary>
    private IntPtr HwndDropDown
    {
        get
        {
            COMBOBOXINFO pcbi = new COMBOBOXINFO();
            pcbi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pcbi);
            NativeMethods.GetComboBoxInfo(this.Handle, ref pcbi);
            return pcbi.hwndList;
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在,我们可以从句柄获取矩形:

    RECT comboBoxRectangle;
    NativeMethods.GetWindowRect((IntPtr)this.HwndCombo, out comboBoxRectangle);
Run Code Online (Sandbox Code Playgroud)

    // get coordinates of combo's drop down list
    RECT dropDownListRectangle;
    NativeMethods.GetWindowRect((IntPtr)this.HwndDropDown, out dropDownListRectangle);
Run Code Online (Sandbox Code Playgroud)

现在我们可以检查:

    if (comboBoxRectangle.Top > dropDownListRectangle.Top)
    {
         ....
    }
Run Code Online (Sandbox Code Playgroud)