0

不确定我是否可以正确地说出来,但我们开始吧。

我的应用程序有几个文本框和一个带有滚动条的面板。我希望鼠标滚轮始终影响面板。有没有办法做到这一点?目前,当我将焦点从面板更改为文本框时,滚轮停止为面板工作。

提前致谢

4

1 回答 1

0

您可以使用 PreFilterMessage 执行此操作。首先,更改您的表单以实现 IMessageFilter,如下所示:

public partial class Form1 : Form, IMessageFilter

然后在构造函数中添加消息过滤器:

public Form1()
{
  InitializeComponent();
  Application.AddMessageFilter(this);
}

然后实现IMessageFilter接口:

public bool PreFilterMessage(ref Message m)
{
  if (m.Msg == WM_MOUSEWHEEL)
  {
    SendMessage(panel1.Handle, m.Msg, m.WParam, m.LParam);
    return true;
  }
  return false;
}

您还需要以下内容:

private const int WM_MOUSEWHEEL = 0x020A;
[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);
于 2012-07-03T03:00:52.760 回答