我有一个 winforms 应用程序,它用多个用户控件填充可滚动区域。 我的问题是,只要该应用程序 View 可见,是否就可以始终捕获鼠标滚轮滚动?当然,这个应用程序正在处于事件状态以获取焦点。
现在,我必须单击出现的所有控件的滚动条,您可以滚动该滚动条以使鼠标滚轮滚动起作用。我想忽略或跳过这一点。我希望能够单击位于可滚动区域中的这些 UserControl 之一的文本字段之一,然后如果我通过鼠标滚轮滚动,则该 UserControl 不应该是尝试滚动的那个,而是这个可滚动的此 UserControl 与所有其他 UserControl 一起放置的区域(父级)。
最佳答案
在主表单中实现IMessageFilter
:
public partial class YourForm : Form, IMessageFilter
{
// Your code.
public bool PreFilterMessage ( ref Message m )
{
if ( m.Msg == 0x20A )
{
NativeMethods.SendMessage ( controlToScroll.Handle , m.Msg , m.WParam , m.LParam );
return true;
}
return false;
}
}
通过在表单的构造函数中调用以下代码,将表单注册为消息过滤器。
Application.AddMessageFilter ( this );
SendMessage
具有以下签名:
internal class NativeMethods
{
[DllImport ( "user32.dll" , CharSet = CharSet.Auto )]
public static extern IntPtr SendMessage ( IntPtr hWnd , Int32 Msg , IntPtr wParam , IntPtr lParam );
}
关于c# - "always"捕获鼠标滚轮事件并滚动父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10530045/