tags:

views:

84

answers:

2

Is there any way (in C#) to display a form with just the minimise and maximise buttons? Without the close button?

The only way of removing the close button (that I'm aware of) is:

form.ControlBox = false;

But this also gets rid of both the other buttons.

+2  A: 

There's an article here showing how to do that. It requires using the unmanaged User32.dll

SnOrfus
Important notes: However, in my experience (Windows 7), that approach "grays" the close button (it doesn't technically remove it) and *does not prevent* the normal Alt+F4 behavior (but you can abort the is-closing for that). You can remove the Min/Max boxes through the window-style, but I think to *completely* remove the close box requires handling the window painting.
pst
+2  A: 

I wrote a function to do this once

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        if (EnableMenuItem(GetSystemMenu(this.Handle, 0), SC_CLOSE, MF_GRAYED) == -1)
            throw new Win32Exception("The message box did not exist to gray out its X");
    }
    private const int SC_CLOSE = 0xF060;
    private const int MF_GRAYED = 0x1;
    [DllImport("USER32")]
    internal static extern int EnableMenuItem(IntPtr WindowHandle, int uIDEnableItem, int uEnable);
    [DllImport("USER32")]
    internal static extern IntPtr GetSystemMenu(IntPtr WindowHandle, int bReset);
}

Note alt-f4 still works and right click "close this window" when you are looking at it from the task bar. (tested in windows 7)

Scott Chamberlain
Add a button and in its Click event set ShowInTaskbar to false to see this fail.
Hans Passant