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);
}
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
- Go to form events-> Location changed
write the following code
Location = new Point(this.Width,this.Height);
Author by
Ozzy
Updated on August 01, 2022Comments
-
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 almost 15 yearsWhat 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 about 13 yearsisn't that possible to relocate the form to it's initial place if it is moved?
-
-
Ozzy almost 15 yearsYea, 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 almost 15 yearsThanks 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 almost 15 yearsWell... the whole point of the program is for it not to be moved lol.
-
Finglas almost 15 yearsHow 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 almost 15 yearsThanks this worked perfectly. Also had a read around the site. Gotta look into the WinAPI more.
-
Failed_Noob about 13 yearsYes I tried that but the form starts blinking a lot when I try to move it to a new place.
-
Oded about 13 years@Failed_Noob - edit your question, post the code and perhaps someone can suggest a fix.
-
Failed_Noob about 13 yearsIn what event should I write this ?
-
V4Vendetta about 13 yearsYou 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 over 11 yearsThere are a lot better answers than this one! If you're going to add a totally different answer, at least explain it!
-
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 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 almost 11 yearsHow can I make it movable again?
-
VAAA almost 11 yearshow can I make it movable again?
-
Smith.Patel almost 11 yearsYou'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 over 6 yearsWell, this does what I wanted. A Fullscreen UI.
-
PeterN about 4 yearsnot works for me! I think you should save values of top and left positions first, then: Location = new Point(LeftPosition,TopPosition);