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 forward packets by netfilter, so I want to get some c
i want to set forward slash line inside my CGRectMake. please help me out.
I think i'm mainly having a brain fart. I just want to fast forward
I want to capture ip packages on one server, and then forward the packages
I simply want to disable Restlet's logging to stdout/stderr in my project and forward
Possible Duplicates: C++, removing #include<vector> or #include<string> in class header Forward declare an STL
I run a website and my subscriber base is gradually increasing. I had to
I have a JSP page that has 2 forms running on Tomcat 6. One
I have a UIButton and a UIView. The View sits above the button, and
I am new to zend framework and having very basic doubt.I have created one

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.