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?
LVL 22
Russ SuterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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.
0
Russ SuterAuthor Commented:
I've already tried this. It does not work.
0
Chinmay PatelChief Technical NinjaCommented:
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.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Russ SuterAuthor 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.
0
Chinmay PatelChief Technical NinjaCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterAuthor Commented:
It's a start. I have more tweaking to do to get this to work just right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.