“总是”捕获mousewheel事件并滚动父级?

时间:2012-05-10 08:19:13

标签: c# winforms user-controls scroll mousewheel

我有一个winforms应用程序,它使用多个UserControl填充可滚动区域。 我的问题是,只要此应用程序视图可见,总是捕获鼠标滚轮滚动吗?当然,这个应用程序是活跃的焦点。

现在我必须单击所有控件的滚动条,您可以滚动到所有控件以使鼠标滚轮工作。我想忽略或跳过这个。我希望能够在其中一个放置在可滚动区域中的UserControl中的其中一个文本字段中单击,然后如果我通过鼠标滚动滚动,则此UserControl不应该是尝试滚动的那个,但是这个可滚动区域(父),其中UserControl与所有其他UserControl一起放置。

1 个答案:

答案 0 :(得分:3)

在主表单中实施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 );
}