tags:

views:

741

answers:

5

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

+1  A: 

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
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
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
+1  A: 

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

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
Well... the whole point of the program is for it not to be moved lol.
Ozzy
+4  A: 

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 = 0×0112;
    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);
}
Jason Down
Thanks this worked perfectly. Also had a read around the site. Gotta look into the WinAPI more.
Ozzy
A: 

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