Creating a immovable windows’ form in c#
June 5, 2006
Recently there were many posts asking about "creating a immovable form in .NET". I guess there are three approaches:
1. Make the property FormBorder=None and repaint the minimize, maximize and close buttons with custom bitmaps and set the event handlers respectively.
2. Keep the FormBorder as it is and try to save the initial position of the form in a Point. Then restore the location to the initial point when ever the Form's LocationChanged event is hit.
private void Form1_Load(object sender, System.EventArgs e)
private void Form1_LocationChanged(object sender,System.EventArgs e)
Me.Location = initLocation;
This causes flickering of the form though the purpose is served.
3. Next the last and complex method is overridding the WinProc to disable the [Move] menuitem from the system menu.
private static extern Int32 EnableMenuItem ( System.IntPtr hMenu , Int32 uIDEnableItem, Int32 uEnable);
private const Int32 HTCAPTION = 0x00000002;
private const Int32 MF_BYCOMMAND =0x00000000;
private const Int32 MF_ENABLED =0x00000000;
private const Int32 MF_GRAYED =0x00000001;
private const Int32 MF_DISABLED =0x00000002;
private const Int32 SC_MOVE = 0xF010;
private const Int32 WM_NCLBUTTONDOWN = 0xA1;
private const Int32 WM_SYSCOMMAND = 0x112;
private const Int32 WM_INITMENUPOPUP = 0x117;
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;
AbleFlags = MF_DISABLED | MF_GRAYED; // disable the move
EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
if(m.Msg==WM_NCLBUTTONDOWN) //cancels the drag this is IMP
if (m.Msg==WM_SYSCOMMAND) // Cancels any clicks on move menu
if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
This is how you can disable the form being moved.