我控制(实现了C#,。Net 2.0)继承自组合框。它有过滤和其他东西。为了保持UI正确,当过滤期间的项目数量下降时,下拉列表会更改其大小以适应剩余的项目数(由NativeMethods.SetWindowPos(...)完成)。
有没有办法检查下拉列表是否显示为向上或向下(字面意思) - 不检查它是否打开,是否打开,但是朝哪个方向,向上或向下?
欢呼,jbk答案 0 :(得分:5)
ComboBox有两个事件(DropDown
和DropDownClosed
)在下拉列表部分打开和关闭时触发,因此您可能希望将处理程序附加到它们以监视控件的状态。 / p>
或者,还有一个布尔属性(DroppedDown
),它应该告诉你当前的状态。
答案 1 :(得分:3)
ComboBoxs向下或向上打开,取决于他们必须打开的空间:如果他们在他们下方有可用空间,他们将像往常一样向下打开,如果不是,他们将向上打开。
因此,您只需检查他们是否有足够的空间来了解它们。试试这段代码:
void CmbTestDropDown(object sender, EventArgs e)
{
Point p = this.PointToScreen(cmbTest.Location);
int locationControl = p.Y; // location on the Y axis
int screenHeight = Screen.GetBounds(new Point(0,0)).Bottom; // lowest point
if ((screenHeight - locationControl) < cmbTest.DropDownHeight)
MessageBox.Show("it'll open upwards");
else MessageBox.Show("it'll open downwards");
}
答案 2 :(得分: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;
}
}
以及组合框的下拉列表:
/// <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;
}
}
现在,我们可以从句柄中获取矩形:
RECT comboBoxRectangle;
NativeMethods.GetWindowRect((IntPtr)this.HwndCombo, out comboBoxRectangle);
和
// get coordinates of combo's drop down list
RECT dropDownListRectangle;
NativeMethods.GetWindowRect((IntPtr)this.HwndDropDown, out dropDownListRectangle);
现在我们可以查看:
if (comboBoxRectangle.Top > dropDownListRectangle.Top)
{
....
}