简体   繁体   English

确定 ComboBox 下拉按钮的边框大小

[英]Determine the size of the bordering rectangle of the ComboBox Drop-Down Button

I would like to be able to check whether the user clicked the Drop-Down Button of the ComboBox in the MouseClick event handler.我希望能够检查用户是否在 MouseClick 事件处理程序中单击了 ComboBox 的下拉按钮。

I could not find the size and coordinates of the bordering rectangle of the Drop-Down Button.我找不到下拉按钮边框的大小和坐标。 I am using WinForms.我正在使用 WinForms。

The task is to check whether the Drop-Down List dropped down (opened, made the items visible) as a result of a programmatic statement: comboBox1.DroppedDown = true;任务是检查下拉列表是否由于编程语句而下拉(打开,使项目可见):comboBox1.DroppedDown = true; or as the result of the user pressing the Drop-Down Button.或作为用户按下下拉按钮的结果。 Is there any other better way to ascertain this?有没有其他更好的方法来确定这一点?

According to your last edit, @dr.null comment might solve your requirement.根据您上次的编辑,@dr.null 评论可能会解决您的要求。 But still you want to make sure that user clicked on the arrow part of the drop down, the following code will do the job.但是您仍然要确保用户单击了下拉菜单的箭头部分,下面的代码将完成这项工作。

在此处输入图像描述

Notice that the position of the dropdown button part will return as the position in the screen.请注意,下拉按钮部分的 position 将在屏幕中返回为 position。

    private void comboBox1_MouseClick(object sender, MouseEventArgs e)
    {
        const int COMBOBOX_DROPDOWN_BUTTON_ACC_ITEM_INDEX = 2;
        var comboBox = ((ComboBox)sender);
        IAccessible accessibilityObjectOfComboBox = comboBox.AccessibilityObject;
        accessibilityObjectOfComboBox.accLocation(
            out int x,
            out int y,
            out int width,
            out int height,
            COMBOBOX_DROPDOWN_BUTTON_ACC_ITEM_INDEX);
        var button = new Rectangle(x, y, width, height);
        var clickedPoint = new Rectangle(new Point(MousePosition.X, MousePosition.Y), new Size(1, 1));
        var IHit = button.IntersectsWith(clickedPoint);
        if (IHit)
            MessageBox.Show("Happy to be helpful");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM