UserControl:如何添加鼠标滚轮监听器

本文关键字:鼠标 监听器 添加 何添加 UserControl | 更新日期: 2023-09-27 18:11:52

我正在创建一个UserControl,当鼠标在控件上并且鼠标滚轮被旋转时,它应该做出反应。

目前我正在这样做:

    public MyUserControl()
    {
        this.MouseWheel += new MouseEventHandler(MouseWheelHandler);
    }
    private void MouseWheelHandler(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Delta > 0)
            incIndex();
        if (e.Delta < 0)
            decIndex();
    }
    protected override void OnMouseEnter(EventArgs e)
    {
        this.Focus();
        base.OnMouseEnter(e);
    }

事实上,这工作得很好,但问题是"this. focus();"的部分,因为它破坏了我的表单/应用程序的行为。

有更好的方法来实现这一点吗?

UserControl:如何添加鼠标滚轮监听器

遇到同样的问题,我最终实现了@Paul_Westcott和@nr1的混合解决方案。这是一个类似于@Paul_Westcott解决方案的本地解决方案(仅适用于订阅的winforms控件)。它是多监视器安全和MDI安全的(由应用程序中的其他窗口重叠)

public static class MouseWheelHandlerForWinformsControl
{
    private class MouseWheelMessageFilter : IMessageFilter
    {
        [DllImport("user32.dll")]
        private static extern IntPtr WindowFromPoint(Point pt);
        private readonly Control mCtrl;
        private readonly Action<MouseEventArgs> mOnMouseWheel;
        public MouseWheelMessageFilter(Control ctrl, Action<MouseEventArgs> onMouseWheel)
        {
            mCtrl = ctrl;
            mOnMouseWheel = onMouseWheel;
        }
        public bool PreFilterMessage(ref Message m)
        {
            // handle only mouse wheel messages
            if (m.Msg != 0x20a)
                return false;
            Point mouseAbsolutePosition = new Point(m.LParam.ToInt32());
            Point mouseRelativePosition = mCtrl.PointToClient(mouseAbsolutePosition);
            IntPtr hControlUnderMouse = WindowFromPoint(mouseAbsolutePosition);
            Control controlUnderMouse = Control.FromHandle(hControlUnderMouse);
            if (controlUnderMouse != mCtrl)
                return false;
            MouseButtons buttons = GetMouseButtons(m.WParam.ToInt32());
            int delta = m.WParam.ToInt32() >> 16;
            var e = new MouseEventArgs(buttons, 0, mouseRelativePosition.X, mouseRelativePosition.Y, delta);
            mOnMouseWheel(e);
            return true;
        }
        private static MouseButtons GetMouseButtons(int wParam)
        {
            MouseButtons buttons = MouseButtons.None;
            if(HasFlag(wParam, 0x0001)) buttons |= MouseButtons.Left;
            if(HasFlag(wParam, 0x0010)) buttons |= MouseButtons.Middle;
            if(HasFlag(wParam, 0x0002)) buttons |= MouseButtons.Right;
            if(HasFlag(wParam, 0x0020)) buttons |= MouseButtons.XButton1;
            if(HasFlag(wParam, 0x0040)) buttons |= MouseButtons.XButton2;
            return buttons;
        }
        private static bool HasFlag(int input, int flag)
        {
            return (input & flag) == flag;
        }
    }
    public static void MemorySafeAdd(Control ctrl, Action<MouseEventArgs> onMouseWheel)
    {
        if (ctrl == null || onMouseWheel == null)
            throw new ArgumentNullException();
        var filter = new MouseWheelMessageFilter(ctrl, onMouseWheel);
        Application.AddMessageFilter(filter);
        ctrl.Disposed += (s, e) => Application.RemoveMessageFilter(filter);
    }
}

一旦将这个助手类添加到解决方案中,就可以在一行中向鼠标滚轮订阅控件myControl,如下所示:

public void Init() {
    MouseWheelHandlerForWinformsControl.MemorySafeAdd(myControl, OnMouseWheelEvent);
}
void OnMouseWheelEvent(MouseEventArgs args) {
    // do what you need here
}

复制自http://social.msdn.microsoft.com/forums/en-US/winforms/thread/eb922ed2-1036-41ca-bd15-49daed7b637c/

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1 {
  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
    }
    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x20a) {
        // WM_MOUSEWHEEL, find the control at screen position m.LParam
        Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
        IntPtr hWnd = WindowFromPoint(pos);
        if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
          SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
          return true;
        }
      }
      return false;
    }
    // P/Invoke declarations
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point pt);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  }
}

这个效果很好,谢谢汉斯·帕桑特

只是在已经发布的解决方案上的一个小变化,以便不必使用p/Invoke。

public static class MouseWheelHandler
{
    public static void Add(Control ctrl, Action<MouseEventArgs> onMouseWheel)
    {
        if (ctrl == null || onMouseWheel == null)
            throw new ArgumentNullException();
        var filter = new MouseWheelMessageFilter(ctrl, onMouseWheel);
        Application.AddMessageFilter(filter);
        ctrl.Disposed += (s, e) => Application.RemoveMessageFilter(filter);
    }
    class MouseWheelMessageFilter
        : IMessageFilter
    {
        private readonly Control _ctrl;
        private readonly Action<MouseEventArgs> _onMouseWheel;
        public MouseWheelMessageFilter(Control ctrl, Action<MouseEventArgs> onMouseWheel)
        {
            _ctrl = ctrl;
            _onMouseWheel = onMouseWheel;
        }
        public bool PreFilterMessage(ref Message m)
        {
            var parent = _ctrl.Parent;
            if (parent != null && m.Msg == 0x20a) // WM_MOUSEWHEEL, find the control at screen position m.LParam
            {
                var pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                var clientPos = _ctrl.PointToClient(pos);
                if (_ctrl.ClientRectangle.Contains(clientPos)
                 && ReferenceEquals(_ctrl, parent.GetChildAtPoint(parent.PointToClient(pos))))
                {
                    var wParam = m.WParam.ToInt32();
                    Func<int, MouseButtons, MouseButtons> getButton =
                        (flag, button) => ((wParam & flag) == flag) ? button : MouseButtons.None;
                    var buttons = getButton(wParam & 0x0001, MouseButtons.Left)
                                | getButton(wParam & 0x0010, MouseButtons.Middle)
                                | getButton(wParam & 0x0002, MouseButtons.Right)
                                | getButton(wParam & 0x0020, MouseButtons.XButton1)
                                | getButton(wParam & 0x0040, MouseButtons.XButton2)
                                ; // Not matching for these /*MK_SHIFT=0x0004;MK_CONTROL=0x0008*/
                    var delta = wParam >> 16;
                    var e = new MouseEventArgs(buttons, 0, clientPos.X, clientPos.Y, delta);
                    _onMouseWheel(e);
                    return true;
                }
            }
            return false;
        }
    }
}

然后这个可以在单独的控件中使用,比如

class MyControl
    : Control
{
    public MyControl()
    {
        ...
        MouseWheelHandler.Add(this, MyOnMouseWheel);
    }
    void MyOnMouseWheel(MouseEventArgs e)
    {
        ...
    }
}

Paul Westcott的答案在使用FlowLayoutPanel时非常有效。我的MyOnMouseWheel事件的实现是:

void MyOnMouseWheel(MouseEventArgs e)
{
    int ChangeIncrement = (this.panel1.VerticalScroll.SmallChange * 4); //Change the 4 to any positive number to scroll more or less one each scroll event.
    if (e.Delta < 0)
    {
        int NewValue = this.panel1.VerticalScroll.Value + ChangeIncrement;
        if (NewValue > this.panel1.VerticalScroll.Maximum)
        {
            this.panel1.VerticalScroll.Value = this.panel1.VerticalScroll.Maximum;
        }
        else
        {
            this.panel1.VerticalScroll.Value = NewValue;
        }
    }
    else if (e.Delta > 0)
    {
        int NewValue = this.panel1.VerticalScroll.Value - ChangeIncrement;
        if (NewValue < this.panel1.VerticalScroll.Minimum)
        {
            this.panel1.VerticalScroll.Value = this.panel1.VerticalScroll.Minimum;
        }
        else
        {
            this.panel1.VerticalScroll.Value = NewValue;
        }
    }
    this.panel1.PerformLayout();
}