c# - "always"捕获鼠标滚轮事件并滚动父级?

标签 c# winforms user-controls scroll mousewheel

我有一个 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/

相关文章:

c# - 占位符控件集合在回发时为空

带有列表的 C# NullReferenceException

c# - Json.Net解析错误如何处理

C# 用户控件 : access controls properties

asp.net - 如何在不使用 <%@ register %> 或 <%@ Reference %> 的情况下动态添加用户控件?

c# - Usercontrol不同控件属性访问

c# - Winforms MVVM GridView 数据绑定(bind)

c# - c# 中的非自动阻塞 MessageBoxes

c# - 如何刷新屏幕以使关闭的表单真正消失

c# - 在 ASP.NET 转发器控件中动态分配用户控件中的属性