Link to home
Start Free TrialLog in
Avatar of Russ Suter
Russ Suter

asked on

C# WinForms: Need a Panel control that will pass through mouse clicks but intercept mouse moves.

Using the example found here: https://stackoverflow.com/questions/547172/pass-through-mouse-events-to-parent-control
I was able to create a Panel control that passes all mouse activity to the underlying control(s). The trouble for me is that it passes ALL mouse activity. I need something that will intercept mouse move behavior but will still allow click events (left click, right click, mouse down, mouse up, and double click) to pass through. Does anyone know how to modify this code to accomplish that task?
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Russ,

You have not posted the code but assuming you have applied

protected override void WndProc(ref Message m)
{
    const int WM_NCHITTEST = 0x0084;
    const int HTTRANSPARENT = (-1);

    if (m.Msg == WM_NCHITTEST)
    {
        m.Result = (IntPtr)HTTRANSPARENT;
    }
    else
    {
        base.WndProc(ref m);
    }
}

Open in new window

as it is, you would want to look at https://docs.microsoft.com/en-us/windows/desktop/inputdev/mouse-input-notifications.

There are many such notifications/window messages that you will find in the list. If you want to only allow clicks I think you should filter based on

//For Left Button
WM_LBUTTONDBLCLK
WM_LBUTTONDOWN
WM_LBUTTONUP
//For Right Button
WM_RBUTTONDBLCLK
WM_RBUTTONDOWN
WM_RBUTTONUP

Let me know if you run into any issues.

Regards,
Chinmay.
Avatar of Russ Suter
Russ Suter

ASKER

I've already tried this. It does not work.
When you say it does not work my request is to put some more details, for example, the code you tried. Did you put in debug points(this is ill-advised though) or traces so we can understand what is going on when your code runs? Do you get any error/s? or simply it does not behave as it should?

I would do one thing if I were you, I would attach a high-performance logger that can dump m.Msg values in a text file. Console.Writeline might work as well but I think it is better to leave Visual Studio alone when dealing with such code. Then check m.Msg values against the values given for each notification message and try to figure out what exactly is going on.
Here's the code I'm currently using
		protected override void WndProc(ref Message m)
		{
			const int WM_NCHITTEST = 0x0084;
			const int WM_MOUSEMOVE = 0x0200;
			const int WM_LBUTTONDOWN = 0x0201;
			const int WM_LBUTTONUP = 0x0202;
			const int HTTRANSPARENT = (-1);
			switch (m.Msg)
			{
				case WM_LBUTTONDOWN:
				case WM_LBUTTONUP:
					m.Result = (IntPtr)HTTRANSPARENT;
					break;
				default:
					base.WndProc(ref m);
					break;
			}
		}

Open in new window

It does nothing that I can tell. Mouse clicks are still captured by the Panel and not passed through to the underlying control. If I use WM_NCHITTEST in place of WM_LBUTTONDOWN and WM_LBUTTONUP it passes ALL mouse events to the underlying control which is not desired.
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's a start. I have more tweaking to do to get this to work just right.