Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

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
0
XWL
Asked:
XWL
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Add this to your form to disable (grey out) the close button (X) on your form:

        protected override CreateParams CreateParams
        {
            get
            {
                const int CS_NOCLOSE = 0x200;
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_NOCLOSE;
                return cp;
            }
        }
0
 
XWLAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you not try my code?  It's much smaller...
0
 
XWLAuthor Commented:
Thank you Idle_Mind! Your solution is better than what I've found!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now