We help IT Professionals succeed at work.

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

Russ Suter
Russ Suter asked
on
717 Views
Last Modified: 2018-09-29
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?
Comment
Watch Question

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
I've already tried this. It does not work.
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
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.
Chief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
It's a start. I have more tweaking to do to get this to work just right.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions