Question

We have a WinForms application which runs on a touch-screen on a bit of industrial equipment. For historical reasons which are not up for changing today, the displayed form has a normal Windows title bar.

We would like to stop people using the mouse (i.e. touchscreen) from moving the window by dragging the title bar. We don't care if there's some other way to move the window using the keyboard.

What's the most elegant way to achieve this? I can think of trying to subvert mouse messages if there's a mouse-down on the titlebar (though NC hit-testing doesn't at first glance seem completely obvious in Winforms), and I can think of responding to Move messages in some way which restores the window position.

But both of these seem clunky, and I have a feeling I am missing something elegant and obvious.

Was it helpful?

Solution

Nc messages are still the go I think. Syncfusion's windows form faq has the code you need. I'd paste a link, but I'm on an iPhone with no copy paste (grumble, grumble!)

OTHER TIPS

Ok, thanks to a bit of encouragement from DanF, I came up with this:

  protected override void  WndProc(ref Message msg)
  {
      const int WM_NCLBUTTONDOWN = 0xa1;

      switch (msg.Msg)
      {
         case WM_NCLBUTTONDOWN:
            // To prevent people moving the window with the mouse 
            // unless CTRL is held
            if (!(GetKeyState((int)Keys.ControlKey) < 0))
            {
               return;
            }
            break;
      }
      base.WndProc(ref msg);
  }

Which seems to be just the thing. Thanks all.

How about modify the main form event's LocationChanged, SizeChanged, etc...

You could force the window to remain maximized, if that's practical for your application.

To handle @rotard's comment on your solution, you can block just movement with the mouse by the following: In your WndProc override, for WM_NCHITTEST, call base.WndProc first. If it returns HTCAPTION, return HTNONE instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top