How do you prevent a windows from being moved?

63,902

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

  1. Go to form events-> Location changed

write the following code

Location = new Point(this.Width,this.Height);
Share:
63,902
Ozzy
Author by

Ozzy

Updated on August 01, 2022

Comments

  • Ozzy
    Ozzy almost 2 years

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

    • LarryB
      LarryB almost 15 years
      What if the user has some other widget in the lower right corner, like the vista sidebar? Do you really want to dictate where your window has to stay? This is by no means userfriendly and I personally wouldn't accept such a restriction.
    • Bastardo
      Bastardo about 13 years
      isn't that possible to relocate the form to it's initial place if it is moved?
  • Ozzy
    Ozzy almost 15 years
    Yea, i figured that it wasnt going to be easy :( The reason i want this is because i want to make a form appear at the bottom right of the screen like a menu. So you can select files etc.
  • Ozzy
    Ozzy almost 15 years
    Thanks for the reply. I only started desktop programming about a month ago so i have no idea how to implement any of what you said.. sorry. I feel ignorant :(
  • Ozzy
    Ozzy almost 15 years
    Well... the whole point of the program is for it not to be moved lol.
  • Finglas
    Finglas almost 15 years
    How I would do it - without looking I don't think there is a property that you can set in .NET to do this so this is your best bet.
  • Ozzy
    Ozzy almost 15 years
    Thanks this worked perfectly. Also had a read around the site. Gotta look into the WinAPI more.
  • Failed_Noob
    Failed_Noob about 13 years
    Yes I tried that but the form starts blinking a lot when I try to move it to a new place.
  • Oded
    Oded about 13 years
    @Failed_Noob - edit your question, post the code and perhaps someone can suggest a fix.
  • Failed_Noob
    Failed_Noob about 13 years
    In what event should I write this ?
  • V4Vendetta
    V4Vendetta about 13 years
    You have to override the WndProc for the Form you desire should not be moved. Type in override and space ,you will get a list wherein you will find the method mentioned above
  • durron597
    durron597 over 11 years
    There are a lot better answers than this one! If you're going to add a totally different answer, at least explain it!
  • Knickedi
    Knickedi about 11 years
    @V4Vendetta maybe because your answer is exactly the same as the accepted one which was posted two years before... I don't understand it either (not my downvote)
  • V4Vendetta
    V4Vendetta about 11 years
    @Knickedi This might be the case where similar question would have been merged along with the answers so you see a huge difference in the timelines.
  • VAAA
    VAAA almost 11 years
    How can I make it movable again?
  • VAAA
    VAAA almost 11 years
    how can I make it movable again?
  • Smith.Patel
    Smith.Patel almost 11 years
    You'd have to have some sort of flag on whether or not to even run the code in the switch statement (which just checks to see whether or not the windows message was to move the window... in which case it returns and does nothing).
  • diomonogatari
    diomonogatari over 6 years
    Well, this does what I wanted. A Fullscreen UI.
  • PeterN
    PeterN about 4 years
    not works for me! I think you should save values of top and left positions first, then: Location = new Point(LeftPosition,TopPosition);