Here is a snippet of code I found on the microsoft.public.dotnet.framework.windowsforms newsgroup to disable the close button on a windows forms application whilst maintaining the minimize and restore buttons..
1) Add a reference to System.Runtime.InteropServices at the top of your form, like so:
using System.Runtime.InteropServices;
2) Add references to the required Win32 functions and constants, like so:
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr GetSystemMenu ( IntPtr hWnd, int bRevert );
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int DrawMenuBar(IntPtr hWnd);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
private const int MF_BYPOSITION = 0x400;
private const int MF_REMOVE = 0x1000;
3) Create the RemoveCloseButton function that does the magic:
private void RemoveCloseButton(Form frmForm)
{
IntPtr hMenu;
int n;
hMenu = GetSystemMenu(frmForm.Handle, 0);
if (!(hMenu == IntPtr.Zero))
{
n = GetMenuItemCount(hMenu);
if (n > 0)
{
RemoveMenu(hMenu, n - 1, MF_BYPOSITION);
RemoveMenu(hMenu, n - 2, MF_BYPOSITION);
}
DrawMenuBar(frmForm.Handle);
}
}
4) Make a call to the RemoveCloseButton function in your form constructor, like so:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
RemoveCloseButton(this);
}
That's it, give it a try and watch in amazement as your close button is disabled.
BTW, here is the link to the original forum post.