How to

disable close 'x' button in winform

Published: 15. December 2008 | Updated: 15. December 2008
License: Microsoft Public License (MS-PL)
Categories: Windows » Win32, Winforms
Tags: C# UI Windows Winforms
Was this snippet helpful for you? YESYES / NONO

Snippet shows how to disable close 'x' button in Winforms

Import namespace

using System.Runtime.InteropServices;

Import extern methods

const int MF_BYPOSITION = 0x400;

[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);

Use

IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
Send us feedback about this snippet »



Related Snippets: