Re: Hide the "X" on a Windows Form

Phil Sayers <[email protected]> Tue, 4 Sep 2007 14:02:47 -0400
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
I get the feeling that plenty of the windows API calls that were used to get
additional functionality in the vb6 days have been lost/forgotten with the
advent of .Net, but there still seems to be a need for some of the
techniques and API calls.


-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]]On Behalf Of
Mike Andrews
Sent: Tuesday, September 04, 2007 1:40 PM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Hide the "X" on a Windows Form


I'd forgotten all about that from my vb6 days.
Yes, you can disable the form's close system menu which will in turn disable
the close button.

I've got a class below that implements the given solution.  However, ALT +
F4 will still cause the form to close so you will have to add keyboard
checks to disable that as well.


    public class FormDisableClose: Form {

        #region PInvoke

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, Int32
bRevert);

        [DllImport("user32.dll")]
        private static extern Int32 GetMenuItemCount(IntPtr hMenu);

        [DllImport("user32.dll")]
        private static extern Int32 DrawMenuBar(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern Int32 RemoveMenu(IntPtr hMenu, Int32
nPosition, Int32 wFlags);

        private const int MF_BYPOSITION = 0x400;
        private const int MF_REMOVE = 0x1000;

        #endregion

        protected override void OnHandleCreated(EventArgs e) {
            base.OnHandleCreated(e);
            IntPtr hMenu = GetSystemMenu(this.Handle, 0);
            if (!hMenu.Equals(IntPtr.Zero)) {
                int n = GetMenuItemCount(hMenu);
                if (n > 0) {
                    RemoveMenu(hMenu, n - 1, MF_BYPOSITION | MF_REMOVE);
                    RemoveMenu(hMenu, n - 2, MF_BYPOSITION | MF_REMOVE);
                    DrawMenuBar(this.Handle);
                }
            }
        }

    }

On 9/4/07, Phil Sayers <[email protected]> wrote:
>
> this might get you close
>
> http://www.thescripts.com/forum/thread362575.html
>
>
>
> -----Original Message-----
> From: Discussion forum for developers using Windows Forms to build apps
> and controls [mailto:[email protected] ]On Behalf Of
> Mike Andrews
> Sent: Tuesday, September 04, 2007 12:55 PM
> To: [email protected]
> Subject: Re: [DOTNET-WINFORMS] Hide the "X" on a Windows Form
>
>
> I don't believe there is a way to do that unless you want to create your
> own
> owner-drawn form.
> You can prevent the form from closing when the x is clicked by using the
> Form_Closing event and setting the Cancel property of the CancelEventArgs
> argument to true.
>
>
> On 9/4/07, Greg Robinson <[email protected]> wrote:
> >
> > How does one hide the "X" (Close) on a Windows From?  I need to hide the
> X
> > but leave the Maximize and Minimize.
> >
> > Thanks,
> >
> >
> >
> > Greg Robinson
> > Custom Data Systems, Inc.
> > www.cds-am.net
> >
>