How do I forward messages (like a mouse wheel) to another control without stealing focus and without P / Invoke?

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

Below is my solution with P / Invoke. I use it only with 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 : complete solution in my answer

+6
source share
3 answers

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

This way you can 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 } 
+7
source

I 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 wheel 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 }); } 
+2
source

It really depends on the type of events and their number. How easy is it to pass events, such as mouse movements, to the parent control (for example, so that the control’s behavior is transparent)?

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

 private void MyControl_MouseMove(object sender, MouseEventArgs e) { if(Parent == null) return; // add this control offsets first so the coordinates fit to the parent control eX += this.Top; eY += this.Left; if(parent.MouseMove != null) parent.MouseMove(sender, e); } 
0
source

Source: https://habr.com/ru/post/888361/


All Articles