6

In my application I need to temporarily gray out the minimize button of the main form. Any ideas how this can be achieved? I don't mind doing p/invokes to Win32 dlls.

Edit: Graying out the minimize button would be the preferred solution, but is there any other way of preventing the form from becoming minimized?

Filip Frącz
  • 5,881
  • 11
  • 45
  • 67

8 Answers8

11

I read your comment in regards to my response and was able to drum up a more complete solution for you. I ran this quickly and it seemed to have the behavior that you wanted. Instead of deriving your winforms from Form, derive from this class:


using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace NoMinimizeTest
{
    public class MinimizeControlForm : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MINIMIZE = 0xf020;

        protected MinimizeControlForm()
        {
            AllowMinimize = true;
        }

        protected override void WndProc(ref Message m)
        {
            if (!AllowMinimize)
            {
                if (m.Msg == WM_SYSCOMMAND)
                {
                    if (m.WParam.ToInt32() == SC_MINIMIZE)
                    {
                        m.Result = IntPtr.Zero;
                        return;
                    }
                }
            }
            base.WndProc(ref m);
        }

        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies whether to allow the window to minimize when the minimize button and command are enabled.")]
        [DefaultValue(true)]
        public bool AllowMinimize
        {
            get;
            set;
        }
    }
}

You could do a bit more if you wanted to be able to decide whether to allow minimizing at the time the click is sent, for instance:


using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace NoMinimizeTest
{
    public class MinimizeControlForm : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MINIMIZE = 0xf020;

        protected MinimizeControlForm()
        {

        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if (m.WParam.ToInt32() == SC_MINIMIZE && !CheckMinimizingAllowed())
                {
                    m.Result = IntPtr.Zero;
                    return;
                }
            }
            base.WndProc(ref m);
        }

        private bool CheckMinimizingAllowed()
        {
            CancelEventArgs args = new CancelEventArgs(false);
            OnMinimizing(args);
            return !args.Cancel;
        }

        [Browsable(true)]
        [Category("Behavior")]
        [Description("Allows a listener to prevent a window from being minimized.")]
        public event CancelEventHandler Minimizing;

        protected virtual void OnMinimizing(CancelEventArgs e)
        {
            if (Minimizing != null)
                Minimizing(this, e);
        }
    }
}

For more information about this window notification, see the MSDN article about it.

Rob
  • 3,276
  • 2
  • 22
  • 25
10
form.MinimizeBox = false;

or if in the form scope

MinimizeBox = false;
Coincoin
  • 27,880
  • 7
  • 55
  • 76
1

Just do MinimizeBox = false; in your form's code.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
1

Put this code in your form's Resize event:

if (this.WindowState == FormWindowState.Minimized)
{
    this.WindowState = FormWindowState.Normal;
}

This will make your form un-minimizable (DISCLAIMER: I do not advocate altering the standard behavior of windows in this way).

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
0

just set the MinimizeBox property of your form to false. this will disable the minimize button but other buttons will remain functional.

0

You can also implement handle to the Minimize event to cancel the command

sebagomez
  • 9,501
  • 7
  • 51
  • 89
0

Don't. Don't mess with my windows. They are mine, not yours. It is my computer and if I want to minimize, I should be able to. I can't think of, and have never been given, a good reason for doing this.

Kevin
  • 7,162
  • 11
  • 46
  • 70
  • 7
    I'm going to go with "nuclear power plant core monitoring software". If the author wants to make that window un-minimizable, I'm fine with it. – MusiGenesis Nov 25 '08 at 23:06
  • 4
    until the monitoring software is showing everything red-lining and the "emergency shutdown" button is located behind the monitoring software window – Kevin Nov 26 '08 at 15:12
  • 8
    For god's sake don't use C# WinForms for nuclear power stations! – Callum Rogers Jan 05 '10 at 16:14
0

Coincoin's answer is correct. MinimizeBox is also available as a property in the designer properties window.

@Kevin: While I appreciate the sentiment, that's not always a valid answer. If the application displays a modal dialog box by creating a new instance of a Form and then calling .ShowDialog() on it, you don't want the user to minimize that Form, because then all input on the main UI thread is blocked until that Form's modal status is satisfied. The user could potentially click on the main form and just get the "ding ding ding" unresponsive sound from Windows and not know what to do.

Rob
  • 3,276
  • 2
  • 22
  • 25
  • Setting MinimizeBox to false would usually suffice, but in my case the MaximizeBox is also hidden. When MinimizeBox is shown, the toolbar renders 3 buttons - minimize, grayed out maximize, close. But when when is set MinimizeBox to false, the minimize/maximize buttons aren't rendered anymore. – Filip Frącz Nov 25 '08 at 23:21
  • or they could need to see a piece of information behind the modal form to fill it out and want to minimize it to see that information better. Also, since this question may be viewed by newbie programmers, I wanted to put this advice out there for them. – Kevin Nov 26 '08 at 15:23