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
XWLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.