using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DisableCloseBtn
{
/// <summary>
/// This class was taken from the following web site:
///
/// http://addressof.com/blog/articles/232.aspx ///
/// Written by Cory Smith in VB. I translated it to C#
/// </summary>
public class CloseButton
{
#region Interop Code
private const int SC_CLOSE=0xF060;
private const int MF_BYCOMMAND=0x0;
private const int MF_GRAYED=0x1;
private const int MF_ENABLED=0x0;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetSystemMenu(IntPtr hWnd, int revert);
[DllImport("user32.dll", SetLastError = true)]
private static extern int EnableMenuItem(int menu, int ideEnableItem, int enable);
#endregion
public static void Disable(Form form)
{
IntPtr hWnd = form.Handle;
int SystemMenu = GetSystemMenu(hWnd,0);
int PreviousState = EnableMenuItem(SystemMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
if(PreviousState == -1)
throw new Exception("The close menu does not exist");
}
}
}