加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – Windows窗体:捕获MouseWheel

发布时间:2020-12-14 01:40:54 所属栏目:Windows 来源:网络整理
导读:我有一个 Windows窗体(在C#.NET中工作). 表单顶部有几个面板,底部有一些ComboBoxes和DataGridViews. 我想在顶部面板上使用滚动事件,但是如果选择例如ComboBox焦点丢失了.面板包含各种其他控件. 当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件? 到
我有一个 Windows窗体(在C#.NET中工作).

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

我想在顶部面板上使用滚动事件,但是如果选择例如ComboBox焦点丢失了.面板包含各种其他控件.

当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件?
到目前为止,我尝试使用MouseEnter / MouseEnter事件,但没有运气.

您所描述的内容听起来像是要复制Microsoft Outlook的功能,在这种情况下,您无需实际单击以聚焦控件以在其上使用鼠标滚轮.

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

这是一个例子(从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);
  }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读