How to forward messages (eg. mouse wheel) to another Control without stealing focus and without P/Invoke?
Asked Answered
E

3

6

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

External answered 17/5, 2011 at 20:46 Comment(3)
While I definitely like your solution a lot more, you're inevitably going to be pinvoking as you can see in the NativeWindow .NET sourcecode: referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/… - nevertheless, +1!Expulsive
Yep. At the time for me it was probably like a challenge because of the frustration of .NET framework not exposing advanced functionalities. Today I would probably have Windows API code pack installed by default in any project.External
Also, today I hardly believe my solution would benefit portability in other .NET implementations like Mono.External
E
7

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
}
External answered 23/5, 2011 at 0:35 Comment(1)
In MSDN, it says 'To ensure proper garbage collection, handles must either be destroyed manually using DestroyHandle or released using ReleaseHandle.'. I haven't seen any thing about this in your answer. Could it be improved better?Karl
T
4

I've found a much simpler solution that can be applied only if the message you are trying to forward has a corresponding event. For example, for the mousewheel event:

// Redirect the mouse wheel event from panel1 to panel2.
// When the panel1 is focused and the mouse wheel is used the panel2 will scroll.
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
   // Get the MouseWheel event handler on panel2
   System.Reflection.MethodInfo onMouseWheel = 
       panel2.GetType().GetMethod("OnMouseWheel", 
                                   System.Reflection.BindingFlags.NonPublic | 
                                   System.Reflection.BindingFlags.Instance);

   // Call the panel2 mousehwweel event with the same parameters
   onMouseWheel.Invoke(panel2, new object[] { e });
}
Then answered 25/1, 2013 at 11:13 Comment(2)
Using reflection here is certainly slow and, IMO, rude. Really forwarding the event message is much more elegant. Moreover, this doesn't really answer my question because in my case I don't know which control need to forward the event while you assume it is panel1.External
Thank you for simple solution, I've been struggling with this. This worked perfectly. I created a custom control and used protected override void OnMouseWheel(MouseEventArgs e) { } @Then #legendTomlin
C
0

It really depends on the kind of events and their number. How about just passing events such as mouse movement to your parent control (e.g. to make the control behave "transparent")?

One of the event handlers in your control could look like this (code out of my head without testing):

private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
    if(Parent == null)
        return;

    // add this control's offsets first so the coordinates fit to the parent control
    e.X += this.Top;
    e.Y += this.Left;
    if(parent.MouseMove != null)
        parent.MouseMove(sender, e);
}
Crossover answered 18/5, 2011 at 0:0 Comment(3)
I can't understand. The problem I have is with such events like mouse wheel or key down that aren't propagated to the control unless it's focused. And can't understand parent.MouseMove(sender, e) because MouseMove delegate can't be invoked directly (it would violate observer pattern otherwise).External
Could you post some more/simplified details about your setup? E.g. where's the control and how it's related to the control/parent that should receive the messages?Crossover
Very easy. Create a form and put a ComboBox and another focusable control inside it. Run the form and focus on the other control. With new MessageForwarder(combobox, 0x20A) of my class, when the mouse is over the combobox it intercepts the mouse wheel so the other control doesn't receive it, without stealing focus from the other control. I'd like to obtain really the same without using P/Invoke.External

© 2022 - 2024 — McMap. All rights reserved.