No Closure
A colleague came to me today asking if I knew of any way to remove the close button from a windows form, but still show the minimize and maximize buttons.
On the face of it, it sounded like an easy answer. I knew you could disable both the minimize and maximize buttons very easily from within the Visual Studio GUI – so surely there’s an option for the third button; the close button?
Actually, no. Whilst you can remove all three buttons in one go (changing the setting “ControlBox” to false.) and as previously mentioned, you can disable the maximize and minimize buttons (“MaximizeBox” and “MinimizeBox” settings respectively), there is no setting to directly control the Close button.
A quick Google search produced a few varied approaches, but the one I suggested my colleague to implement is as follows:
1 2 3 4 5 6 7 8 9 10 | private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams mdiCp = base.CreateParams;
mdiCp.ClassStyle = mdiCp.ClassStyle | CP_NOCLOSE_BUTTON;
return mdiCp;
}
} |
This small bit of code simply needs to be placed within your form class; It overrides a property that is already called when a form is initialised, so doesn’t need to be explicitly called.
Some before and after shots: