Link to home
Start Free TrialLog in
Avatar of XWL
XWL

asked on

How to disable or remove CLoseBox on the title bar?

Hi there,

I would like to disable or remove the Close Box on the title bar of my windows application. However, I want Max/Min boxes remain there.

Could anybody here tell me how? I am using C#.NET/Visual Studio 2005/Win2k.

Thank you,

XWL
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XWL
XWL

ASKER

Hi there,

I have found a way to achieve it (not best, it works though):

In the class Form:

//**************************************************
//** Win32 method imports to disable CloseBxo on title bar
//**************************************************
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int DeleteMenu( IntPtr hMenu, uint uiPosition, uint uiFlags );

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int DrawMenuBar(IntPtr hWnd)

private const int _iSC_CLOSE = 0XF060;
private const int _iMF_GRAYED = 0x0;

In Form_Load Method:

private void Form_Load(object sender, EventArgs e)
{
   // Disable the CloseBox on the title bar
   DeleteMenu(GetSystemMenu(this.Handle, false), _iSC_CLOSE,_iMF_GRAYED);
   DrawMenuBar(this.Handle);
}


The code above makes the Cloase Box on the applicatrion title bar disabled. That’s all.

One thing is strange to me that I tried using Win32 EnableMenuItem function and it doesn’t work for my purpose.

Thank you,

XWL
Did you not try my code?  It's much smaller...
Avatar of XWL

ASKER

Thank you Idle_Mind! Your solution is better than what I've found!