防止在组合框中滚动鼠标悬停

问题描述

当鼠标悬停在组合框上时(鼠标悬停),我想防止在组合框中滚动。我已经尝试了以下解决方案,但它只会在组合框展开时停止滚动。

C# - how do I prevent mousewheel-scrolling in my combobox?

解决方法

您需要扩展 ComboBox 控件,并抑制 MouseWheel 消息。

像这样:

public class ComboBoxEx : ComboBox
{
    protected override void WndProc(ref Message m) 
    {
        if (m.Msg == 522 /* WM_MOUSEWHEEL */) 
        {
            return;
        }

        base.WndProc(ref m);
    }
}
,
 public static void Combo_MouseWheel(object sender,MouseEventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;
            if (!cmb.DroppedDown)
            {
                ((HandledMouseEventArgs)e).Handled = true;
                //this.Focus();
            }
        }