What is the best way to stop a Winforms window being moved around

We have a WinForms application that runs on a touch screen with a small amount of industrial equipment. For historical reasons that do not change today, the displayed form has the usual Windows title bar.

We would like people using the mouse (i.e. touch screen) not to move the window by dragging the title bar. We don't care if there is another way to move the window using the keyboard.

What is the most elegant way to achieve this? I can think of trying to undermine the mouse messages if the mouse pointer is on the title bar (although NC testing at first glance seems quite obvious in Winforms), and I can think of somehow responding to Move messages restores the position of the window.

But both of these seem awkward, and I feel like I'm missing something elegant and obvious.

+3
source share
5 answers

Nc messages are still coming, I think. Faq form window synchronization has the code you need. I would embed the link, but I am on the iPhone without copying (grumbling, grumbling!)

+3
source

Ok, thanks a bit of support 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);
  }

, , . .

+2

How about changing the main event of the form LocationChanged, SizeChanged, etc.

0
source

You can force the window to remain maximized if it is practical for your application.

0
source

To process the @rotard comment for your solution, you can block the movement only with the mouse as follows: in your WndProcredefinition, WM_NCHITTESTfirst call base.WndProc. If it returns HTCAPTION, return HTNONE.

0
source

Source: https://habr.com/ru/post/1698669/


All Articles