Windows 窗体:捕获 MouseWheel

发布于 2024-10-14 08:50:18 字数 226 浏览 1 评论 0原文

我有一个 Windows 窗体(在 C#.NET 中工作)。

该表单的顶部有几个面板,底部有一些 ComboBox 和 DataGridView。

我想使用顶部面板上的滚动事件,但如果选择例如组合框,焦点就会丢失。这些面板包含各种其他控件。

当鼠标悬停在任何面板上时,如何才能始终接收鼠标滚轮事件? 到目前为止,我尝试使用 MouseEnter / MouseEnter 事件,但没有成功。

I have a Windows Form (working in C#.NET).

The form has a couple of panels top-side and some ComboBoxes and DataGridViews bottom-side.

I want to use the scroll events on the top-side panels, but if selecting a e.g. ComboBox the focus is lost. The panels contain various other controls.

How could I always receive the mouse wheel events when the mouse is over any of the panels ?
So far I tried to use the MouseEnter / MouseEnter events but with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-10-21 08:50:18

您所描述的内容听起来像是您想要复制 Microsoft Outlook 等功能,您不需要实际单击来聚焦控件即可使用鼠标滚轮。

这是一个需要解决的相对高级的问题:它涉及实现包含表单的 IMessageFilter 接口,查找 WM_MOUSEWHEEL 事件并将它们定向到鼠标悬停在其上的控件。

这是一个示例(来自 这里):

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());
        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);
  }
}

请注意,此代码对应用程序中的所有表单都有效,而不仅仅是主表单。

What you describe sounds like you want to replicate the functionality of for example Microsoft Outlook, where you don't need to actually click to focus the control to use the mouse wheel on it.

This is a relatively advanced problem to solve: it involves implementing the IMessageFilter interface of the containing form, looking for WM_MOUSEWHEEL events and directing them to the control that the mouse is hovering over.

Here's an example (from here):

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());
        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);
  }
}

Note that this code is active for all the forms in your application, not just the main form.

一身仙ぐ女味 2024-10-21 08:50:18

每个控件都有一个鼠标滚轮事件,当控件具有焦点时鼠标滚轮移动时会发生该事件。

查看以下内容以获取更多信息:Control.MouseWheel 事件

Every control has a mousewheel event that occurs when the mouse wheel moves while the control has focus.

Check this out for more info: Control.MouseWheel Event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文