views:

249

answers:

4

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.

A: 

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

kenny
A: 

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

Erick B
+3  A: 

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!)

Dan F
+2  A: 

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.

Will Dean
Steve Dunn
If you re-read the question, you'll see I did say "We don't care if there's some other way to move the window using the keyboard."
Will Dean