Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5953083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:46:26+00:00 2026-05-22T17:46:26+00:00

I want to forward a message (such as WM_MOUSEWHEEL) when I’m over this control

  • 0

I want to forward a message (such as WM_MOUSEWHEEL) when I’m over this control with the mouse, without stealing the focus. This problem can be easily solved intercepting the message with an IMessageFilter (to be added to the application message pump) and forwarding it with the P/Invoke(d) SendMessage(). The question is: can I do the same without using P/Invoke (solutions I’ve found in StackOverflow use P/Invoke)? If not, why?

The code below is my solution with P/Invoke. I use it with just new MessageForwarder(control, 0x20A).

/// <summary>
/// This class implements a filter for the Windows.Forms message pump allowing a
/// specific message to be forwarded to the Control specified in the constructor.
/// Adding and removing of the filter is done automatically.
/// </summary>
public class MessageForwarder : IMessageFilter
{
#region Fields

private Control _Control;
private Control _PreviousParent;
private HashSet<int> _Messages;
private bool _IsMouseOverControl;

#endregion // Fields

#region Constructors

public MessageForwarder(Control control, int message)
    : this(control, new int[] { message }) { }

public MessageForwarder(Control control, IEnumerable<int> messages)
{
    _Control = control;
    _Messages = new HashSet<int>(messages);
    _PreviousParent = control.Parent;
    _IsMouseOverControl = false;

    control.ParentChanged += new EventHandler(control_ParentChanged);
    control.MouseEnter += new EventHandler(control_MouseEnter);
    control.MouseLeave += new EventHandler(control_MouseLeave);
    control.Leave += new EventHandler(control_Leave);

    if (control.Parent != null)
        Application.AddMessageFilter(this);
}

#endregion // Constructors

#region IMessageFilter members

public bool PreFilterMessage(ref Message m)
{
    if (_Messages.Contains(m.Msg) && _Control.CanFocus && !_Control.Focused
        && _IsMouseOverControl)
    {
        SendMessage(_Control.Handle, m.Msg, m.WParam, m.LParam);
        return true;
    }

    return false;
}

#endregion // IMessageFilter

#region Event handlers

void control_ParentChanged(object sender, EventArgs e)
{
    if (_Control.Parent == null)
        Application.RemoveMessageFilter(this);
    else
    {
        if (_PreviousParent == null)
            Application.AddMessageFilter(this);
    }
    _PreviousParent = _Control.Parent;
}

void control_MouseEnter(object sender, EventArgs e)
{
    _IsMouseOverControl = true;
}

void control_MouseLeave(object sender, EventArgs e)
{
    _IsMouseOverControl = false;
}

void control_Leave(object sender, EventArgs e)
{
    _IsMouseOverControl = false;
}

#endregion // Event handlers

#region Support

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

#endregion // Support

}

EDIT: Full solution in my answer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:46:27+00:00Added an answer on May 22, 2026 at 5:46 pm

    Found a method: you have to inherit NativeWindow, assign the handle of the chosen control to it an call the protected WndProc after you have intercepted a message in any way you prefer (in my case, the inherited class is even an IMessageFilter so I can easily plug it to the application). I use it with new MessageForwarder(anycontrol, 0x20A) to redirect mouse wheel.

    So it’s possible to intercept and forward messages to any control without p/invoke. It was well hidden though.

    /// <summary>
    /// This class implements a filter for the Windows.Forms message pump allowing a
    /// specific message to be forwarded to the Control specified in the constructor.
    /// Adding and removing of the filter is done automatically.
    /// </summary>
    public class MessageForwarder : NativeWindow, IMessageFilter
    {
        #region Fields
    
        private Control _Control;
        private Control _PreviousParent;
        private HashSet<int> _Messages;
        private bool _IsMouseOverControl;
    
        #endregion // Fields
    
        #region Constructors
    
        public MessageForwarder(Control control, int message)
            : this(control, new int[] { message }) { }
    
        public MessageForwarder(Control control, IEnumerable<int> messages)
        {
            _Control = control;
            AssignHandle(control.Handle);
            _Messages = new HashSet<int>(messages);
            _PreviousParent = control.Parent;
            _IsMouseOverControl = false;
    
            control.ParentChanged += new EventHandler(control_ParentChanged);
            control.MouseEnter += new EventHandler(control_MouseEnter);
            control.MouseLeave += new EventHandler(control_MouseLeave);
            control.Leave += new EventHandler(control_Leave);
    
            if (control.Parent != null)
                Application.AddMessageFilter(this);
        }
    
        #endregion // Constructors
    
        #region IMessageFilter members
    
        public bool PreFilterMessage(ref Message m)
        {
            if (_Messages.Contains(m.Msg) && _Control.CanFocus && !_Control.Focused
                && _IsMouseOverControl)
            {
                m.HWnd = _Control.Handle;
                WndProc(ref m);
                return true;
            }
    
            return false;
        }
    
        #endregion // IMessageFilter
    
        #region Event handlers
    
        void control_ParentChanged(object sender, EventArgs e)
        {
            if (_Control.Parent == null)
                Application.RemoveMessageFilter(this);
            else
            {
                if (_PreviousParent == null)
                    Application.AddMessageFilter(this);
            }
            _PreviousParent = _Control.Parent;
        }
    
        void control_MouseEnter(object sender, EventArgs e)
        {
            _IsMouseOverControl = true;
        }
    
        void control_MouseLeave(object sender, EventArgs e)
        {
            _IsMouseOverControl = false;
        }
    
        void control_Leave(object sender, EventArgs e)
        {
            _IsMouseOverControl = false;
        }
    
        #endregion // Event handlers
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
this may sound pretty straight forward, but still I want to post this question
I am building a client-server application. Now I want to forward the message from
I want to use message-forwarding on my SZNUnmanagedReference class. It has this properties: @property
I want to forward declare a static member function of a class in another
Really simple question: From within GWT I want to forward the user away from
When the user running my application, I want my application forward the user to
On default I want my struts2 app to forward to an action: <?xml version=1.0
customers does not want to allow user to use back or forward button. Just
Is there any straight forward way to do that? I want to give an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.