jacknad
jacknad

Reputation: 13739

C#: how to disable form move?

The MSV-Studio description for Locked is "The Locked property determines if we can move or resize the control" so I set the winforms Locked property to true but the form is still movable. What is the correct way to prevent the form from moving?

Upvotes: 5

Views: 12449

Answers (4)

Eric Mickelsen
Eric Mickelsen

Reputation: 10377

Maximize it. Thanks, JackN. ;-)

Upvotes: 6

dotalchemy
dotalchemy

Reputation: 2467

I use the following code to display a form dialog window for a corporate security application written in-house - one of the requirements was that the form could not be moved, resized or live under any other form. Anyway, see below for a start...

/// <seealso href="http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx"/>
    /// <seealso href="http://msdn.microsoft.com/en-us/library/ms633545(v=vs.85).aspx"/>
    public class ShowMessage
    {
        const int SW_SHOWMAXIMIZED = 3; //for maximising (if desired)
        const int SW_SHOW = 5; //for simply activating the form (not needed)
        const int SW_SHOWNORMAL = 1; //displays form at original size and position (what we use here)

        const UInt32 SWP_NOSIZE = 0x0001; //cannot be resized
        const UInt32 SWP_NOMOVE = 0x0002; //cannot be moved

        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //always lives at the top
        const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; //sets the flags for no resize / no move

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        /// <summary>
        /// Displays the passed form using the parameters set in the base ShowMessage class
        /// </summary>
        /// <param name="frm">A Windows Form object</param>
        /// <example><code>ShowMessage.ShowTopmost(new myForm());</code></example>
        public static void ShowTopmost(Form frm)
        {
            ShowWindow(frm.Handle, SW_SHOWNORMAL); //shows the form
            SetWindowPos(frm.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); //sets the form position as topmost, centered

        }
    }

Then I simply call

ShowMessage.ShowTopmost(new frmMessage());

I'm not saying it's the only way or the right way, but it a way to do it.

Upvotes: 3

Tony Abrams
Tony Abrams

Reputation: 4673

You might be able to use the Move event of the form and set the form back to the starting position. You would have to capture and store (in memory) the starting position.

Upvotes: 0

McKay
McKay

Reputation: 12614

It's generally bad form to prevent the user from moving the window. The user should be able to have the window wherever he wants. Preventing resizing is one thing, preventing moving is another. I'm not aware of any C# native way of doing this, but you can probably hook down into Win32 to prevent the window from moving.

Upvotes: 1

Related Questions