Ken Kopczyk

Hurdles in .NET Development

Disabling and Enabling the Close Button in .NET 2.0 WinForms

Posted by Ken on January 23, 2010

In .NET 2.0 WinForms, the user is given the following control over the minimize/maximize/close buttons (otherwise known as the ControlBox):

  • Disable/enable the Minimize button via the Form.MinimizeBox property
  • Disable/enable the Maximize button via the Form.MaximizeBox property
  • Show/hide the entire ControlBox via the Form.ControlBox property

However, you may want to allow maximizing and minimizing, but remove the user’s ability to close the application using the close button for any number of reasons. For example:

  • You want to run some process that cannot be cancelled midway through.
  • You want to ensure a user completes some number of tasks before being able to exit.

Unfortunately, this functionality is not supported by default. One must leverage the unmanaged code of the Windows API to achieve this behavior:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class SuperForm : Form
{ 
    private const int MF_BYPOSITION = 0x400;

    protected void DisableCloseButton()
    {
        IntPtr hMenu = GetSystemMenu(this.Handle, false);
        int menuItemCount = GetMenuItemCount(hMenu);
        RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
        DrawMenuBar((int)this.Handle);
    }
    protected void EnableCloseButton()
    {
        GetSystemMenu(this.Handle, true);
        DrawMenuBar((int)this.Handle);
    }

    // Win32 API declarations
    [DllImport("User32")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport("User32")]
    private static extern int GetMenuItemCount(IntPtr hWnd);
    [DllImport("User32")]
    private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
    [DllImport("User32")]
    private static extern IntPtr DrawMenuBar(int hwnd);
}

Notice that I incorporated this functionality into a base class that all your forms can inherit from to gain this ability.

6 Responses to “Disabling and Enabling the Close Button in .NET 2.0 WinForms”

  1. Mark said

    Nice tutorial.

    Do you know any or all of the implications of using unmanaged code? Recently I was trying to bundle all the files required dlls for a small app I wrote so that it could easily be distributed. Unfortunately it was dependent upon a third party library that had unmanaged code and every technique I read online and tried was unable to embed it.

    I am curious what the ramifications are of using unmanaged code.

    • Ken said

      What I’m curious about is what actual problems you were having. Was it the distribution that was the problem or the actual invocation of the 3rd party library during development? What was did this 3rd party software do?

      I have an extremely similar scenario that I work with and have solved it on both ends. Let me know!

      • Mark said

        Basically we purchased a 3rd party DLL that manipulates PDFs. I tried some software from Microsoft that is supposed to embed DLLs in the executable but this software did not work due to the fact that the DLL contained unmanaged code.

        I also tried a technique that involved treating the DLL as a resource and dynamically loading the assembly with reflection. I forget the specific wall I ran into with this approach but I suspect it was related to security issues.

        • Ken said

          Here’s what I would do:
          1. Put the 3rd party dll into your Windows\System32 directory and register it by running this command:

          regsvr32 /i foo.dll
          

          2. Add a reference to the dll via Visual Studio as you normally would. It should create a .NET dll called Interop.foo.dll when building. See my previous post for info for this.
          3. Invoke in the code.
          4. For deployment, use a Setup Project (vdproj). Add the dll to “System Folder” and register it as vsdrfCOM. Also add your project output (obviously).

  2. domain said

    This piece of writing offers clear idea in favor of the new users of blogging, that
    genuinely how to do running a blog.

  3. Good way of describing, and pleasant piece of writing to obtain facts about my presentation subject, which i am going to convey in institution of higher education.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: