40

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

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Ozzy
  • 10,285
  • 26
  • 94
  • 138
  • What do you mean by being moved? Do you mean not being able to drag it to another location on the desktop? – Jason Down May 25 '09 at 19:45
  • isn't that possible to relocate the form to it's initial place if it is moved? – Bastardo May 02 '11 at 06:33
  • 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. – VVS May 25 '09 at 20:08

15 Answers15

75

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);
}
rpattabi
  • 9,984
  • 5
  • 45
  • 53
Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • Thanks this worked perfectly. Also had a read around the site. Gotta look into the WinAPI more. – Ozzy May 25 '09 at 20:10
  • 2
    How can I make it movable again? – VAAA Aug 19 '13 at 16:05
  • 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). – Jason Down Aug 19 '13 at 18:54
28

You can set the FormBorderStyle property of the Form to None

this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None
crypted
  • 10,118
  • 3
  • 39
  • 52
10

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

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • In what event should I write this ? – Failed_Noob May 02 '11 at 07:01
  • 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 – V4Vendetta May 02 '11 at 07:05
  • @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) – Knickedi Apr 02 '13 at 22:53
  • @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. – V4Vendetta Apr 03 '13 at 04:48
3

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);
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
2

Just change the FormBorderStyle property to None.

Honza Pačuta
  • 317
  • 6
  • 18
Hamza Javed
  • 69
  • 1
  • 2
  • 12
2

It's not a good practice to make your form immovable. I'd think agfain about it if I were you.
Anyway, you can do this by overridding the WinProc to disable the [Move] menuitem from the system menu.

[DllImport("user32.dll")]
private static extern Int32 EnableMenuItem ( System.IntPtr hMenu , Int32uIDEnableItem, Int32 uEnable);  
private const Int32 HTCAPTION = 0×00000002;  
private const Int32 MF_BYCOMMAND =0×00000000;  
private const Int32 MF_ENABLED =0×00000000;  
private const Int32 MF_GRAYED =0×00000001;  
private const Int32 MF_DISABLED =0×00000002; 
private const Int32 SC_MOVE = 0xF010; 
private const Int32 WM_NCLBUTTONDOWN = 0xA1;
private const Int32 WM_SYSCOMMAND = 0×112;
private const Int32 WM_INITMENUPOPUP = 0×117;

protected override void WndProc(ref System.Windows.Forms.Message m )
{
if (m.Msg == WM_INITMENUPOPUP)
{
    //handles popup of system menu
    if ((m.LParam.ToInt32() / 65536) != 0) // 'divide by 65536 to get hiword
    {
        Int32 AbleFlags = MF_ENABLED;
        if (!Moveable)
        {
            AbleFlags = MF_DISABLED | MF_GRAYED; // disable the move
        }
        EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
    }
}
if (!Moveable)
{
    if (m.Msg == WM_NCLBUTTONDOWN) //cancels the drag this is IMP
    {
        if (m.WParam.ToInt32() == HTCAPTION) return;
    }
    if (m.Msg == WM_SYSCOMMAND) // Cancels any clicks on move menu
    {
        if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
    }
}
base.WndProc(ref m);
}  

Also, you can handle OnMove event of your form. But I think this will cause some flickering:

private void Form1_Move(object sender, EventArgs e)
{
    this.Location = defaultLocation;
}
Kamyar
  • 18,639
  • 9
  • 97
  • 171
2

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?

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 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 May 25 '09 at 19:49
  • 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. – Finglas May 25 '09 at 20:00
2

In Windows, the WS_CAPTION style is the non-client area that allows your window to be moved with a mouse. So the easiest way to do what you want is to remove this style from your window.

However, if you need to have a caption and still achieve what you want, then the next style would be to capture the WM_NCHITTEST message and check for HTCAPTION. If the code is HTCAPTION, return NTNOWHERE instead. This will prevent the default window procedure from executing the default move window thing.

Tommy Hui
  • 1,306
  • 6
  • 9
  • 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 May 25 '09 at 19:50
1

change the Form property StartPostion to Manual. Then, handle the LocationChanged event:

private void frmMain_LocationChanged(object sender, EventArgs e)
{
Location = new Point(0, 0);
}
sayadyanh
  • 43
  • 1
  • 3
1
  1. Go to form events-> Location changed

write the following code

Location = new Point(this.Width,this.Height);
Prasad Bhosale
  • 682
  • 8
  • 20
  • not works for me! I think you should save values of top and left positions first, then: Location = new Point(LeftPosition,TopPosition); – PeterN Apr 27 '20 at 11:42
0

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.

Riftus
  • 59
  • 1
  • 10
0

Private Sub MyFormLock() Me.Location = New Point(0, 0) End Sub

Private Sub SearchSDR_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    Call MyFormLock()
End Sub
0

You can try:

this.Locked = true;
sergiol
  • 4,122
  • 4
  • 47
  • 81
  • 2
    There are a lot better answers than this one! If you're going to add a totally different answer, at least explain it! – durron597 Dec 05 '12 at 20:27
0

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

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

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.

David McEwing
  • 3,320
  • 18
  • 16