How do you prevent a windows from being moved?

前端 未结 15 1975
刺人心
刺人心 2020-11-27 15:30

How would i go about stopping a form from being moved. I have the form border style set as FixedSingle and would like to keep it this way because it looks good in vista :)

相关标签:
15条回答
  • 2020-11-27 16:07

    Take a look at this link. You might be interested in option #3. It will require you to wrap some native code, but should work. There's also a comment at the bottom of the link that shows an easier way to do it. Taken from the comment (can't take credit for it, but I'll save you some searching):

    protected override void WndProc(ref Message message)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;
    
        switch(message.Msg)
        {
            case WM_SYSCOMMAND:
               int command = message.WParam.ToInt32() & 0xfff0;
               if (command == SC_MOVE)
                  return;
               break;
        }
    
        base.WndProc(ref message);
    }
    
    0 讨论(0)
  • 2020-11-27 16:07

    I would question your need to make the form unmovable. This doesn't sound nice. You could of course save the location of the window when the window closes and reopen the window into that position. That gives the user some control over where the window should be located.

    0 讨论(0)
  • 2020-11-27 16:08

    Just change the FormBorderStyle property to None.

    0 讨论(0)
  • 2020-11-27 16:09

    You can subscribe to the Form.Move event and reposition from it.

    0 讨论(0)
  • 2020-11-27 16:12

    You can set the FormBorderStyle property of the Form to None

    this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None
    
    0 讨论(0)
  • 2020-11-27 16:14

    Just reset the location on formlocation_changed event to where it was i.e. set the Form.Location to a variable before it's moved and when the user tries to move it, it will go back to the variable location you set it to.

    0 讨论(0)
提交回复
热议问题