Jonas Lindahl
Jonas Lindahl

Reputation: 802

"always" capture mousewheel event and scroll parent?

I have a winforms application that populates an scrollable area with several UserControls. My question, is there anyway to always capture mousewheel scroll as long as this application view is visible? and of course this application being active for focus.

Right now I have to click on the scrollbar that appears for all the controls you can scroll trough to make mousewheel scrolling work. This I would like to ignore or skip. I want to be able to have clicked in one of the text-fields in one of these UserControls that are placed in the scrollable area, and then if I scroll by mousewheel, this UserControl should not be the one trying to scroll, but this scrollable area (parent) where this UserControl is placed with all the other UserControls.

Upvotes: 2

Views: 1691

Answers (1)

Thomas
Thomas

Reputation: 1321

Implement IMessageFilter in your main form:

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;
    }
}

Register your form as a message filter by calling the following in it's constructor.

Application.AddMessageFilter ( this );

SendMessage has the following signature:

internal class NativeMethods
{
    [DllImport ( "user32.dll" , CharSet = CharSet.Auto )]
    public static extern IntPtr SendMessage ( IntPtr hWnd , Int32 Msg , IntPtr wParam , IntPtr lParam );
}

Upvotes: 3

Related Questions