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

Russ Suter
Russ Suter used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

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

Author

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

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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Russ SuterSenior Software Developer

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
Distinguished Expert 2018
Commented:
Hi Russ,

I have been playing with this code for a while now and here are my findings.

This code definitely works but it presents some weird results. For example, It is able to block out the Click and Move etc when I tweaked your code a bit but if I want to click a button below it, somehow it does not work as expected - maybe I am missing something. I did not implement the floating window but I just put a panel above a button so that half of the button is visible and I used the following code

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_MOUSEMOVE:
                    Console.WriteLine(string.Concat("Message:\t\t", m.Msg, "\t\tLPARAM:\t\t", m.LParam, "\t\tWParam:\t\t", m.WParam, "\t\tResult:\t\t", m.Result));
                    m.Result = (IntPtr)HTTRANSPARENT;
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

Open in new window


I could filter MouseMove i.e. there were no console updates on the mouse move BUT it did not click through when I clicked on the panel.

Can you check on your end if this is the case for you as well i.e. all the commands are being blocked?

Regards,
Chinmay.
Russ SuterSenior Software Developer

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial