0

I have a windows form that I want to make it non movable when the user clicks a button and make it movable again when the user clicks again the button.

I found this solution here: How do you prevent a windows from being moved?

But its an override so I think that is for making the form non movable for ever.

Any clue? Thanks

Community
  • 1
  • 1
VAAA
  • 14,531
  • 28
  • 130
  • 253
  • Just stick a flag in the overridden code that skips the code when set one way, or runs it when set the other way (the linked code simply ignores SC_MOVE messages, you can still override and optionally maintain the original functionality if you keep the call to base.WndProc()) – Charleh Aug 19 '13 at 16:14
  • use the solution you find, and just add in another condition (your button clicked) – Bolu Aug 19 '13 at 16:14
  • i think that solution should work for you – Jonesopolis Aug 19 '13 at 16:14
  • 3
    IMO don't prevent user from moving a form. End user will feel app got hung! – Sriram Sakthivel Aug 19 '13 at 16:17

1 Answers1

3

Just have a flag:

private bool _preventMove = false;

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0×0112;
    const int SC_MOVE = 0xF010;

    if(_preventMove) 
    {
        switch(message.Msg)
        {
            case WM_SYSCOMMAND:
               int command = message.WParam.ToInt32() & 0xfff0;
               if (command == SC_MOVE)
                  return;
               break;
        }
    }

    base.WndProc(ref message);
}

Set the flag to true/false to disable/enable movement

Charleh
  • 13,749
  • 3
  • 37
  • 57
  • I will place that in my BaseForm class, so MainForm is BaseForm do I have to do MainForm.Invalidate() in order to execute it? – VAAA Aug 19 '13 at 17:40
  • No idea - not tried to hook WndProc before but I assume that messages are pumped pretty continuously from the queue so I'd imagine that on it's own will be enough – Charleh Aug 19 '13 at 20:08