We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to disable or remove CLoseBox on the title bar?

XWL
XWL asked
on
Medium Priority
440 Views
Last Modified: 2008-01-09
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
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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;
            }
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
XWL

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Did you not try my code?  It's much smaller...
XWL

Author

Commented:
Thank you Idle_Mind! Your solution is better than what I've found!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.