How do you prevent a windows from being moved?

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


Solution 1:

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);
}

Solution 2:

You can set the FormBorderStyle property of the Form to None

this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None

Solution 3:

I found this to stop the form from moving (its in c#)

protected override void WndProc(ref Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;

            switch (m.Msg)
            {
                case WM_SYSCOMMAND:
                    int command = m.WParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                        return;
                    break;
            }
            base.WndProc(ref m);
        }

Found here

Solution 4:

Try to override WndProc:

protected override void WndProc(ref Message m)
{
   const int WM_NCLBUTTONDOWN = 161;
   const int WM_SYSCOMMAND = 274;
   const int HTCAPTION = 2;
   const int SC_MOVE = 61456;

   if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
   {
       return;
   }

   if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
   {
       return;
   }

   base.WndProc(ref m);
}

Solution 5:

It's not all pretty (there is some flashing going on when you try to move the form), but you can use the LocationChanged property to keep the form where you want it:

private Point _desiredLocation;
// assign the _desiredLocation variable with the form location at some
// point in the code where you know that the form is in the "correct" position


private void Form_LocationChanged(object sender, EventArgs e)
{
    if (this.Location != _desiredLocation)
    {
        this.Location = _desiredLocation;
    }
}

Out of curiousity; why would you want to do this?