首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows窗体:捕获MouseWheel

Windows窗体:捕获MouseWheel
EN

Stack Overflow用户
提问于 2011-01-23 02:53:45
回答 2查看 4.4K关注 0票数 6

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

表单顶部有几个面板,底部有一些ComboBoxes和DataGridViews。

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

当鼠标位于任何面板上时,我如何总是收到鼠标滚轮事件?到目前为止,我尝试使用MouseEnter / MouseEnter事件,但没有成功。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-23 03:11:36

您所描述的听起来像是您想要复制Microsoft Outlook的功能,在Microsoft Outlook中,您不需要实际单击以使控件成为焦点即可在其上使用鼠标滚轮。

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

下面是一个示例(来自here):

代码语言:javascript
复制
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);
  }
}

请注意,此代码对应用程序中的所有窗体都是活动的,而不仅仅是主窗体。

票数 13
EN

Stack Overflow用户

发布于 2011-01-23 03:00:10

每个控件都有一个鼠标滚轮事件,该事件在控件具有焦点的情况下移动鼠标滚轮时发生。

有关更多信息,请查看此处:Control.MouseWheel Event

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4769854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档