Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the minimize button in C#?

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?

like image 921
Filip Frącz Avatar asked Nov 25 '08 22:11

Filip Frącz


People also ask

How do you remove minimize and maximize button in Windows Form C#?

To remove maximize button we have to set the MaximizeBox property to false of the windows form. Now when you open the windows form you will notice the windows form's maximize button is in disable mode as well as if user double-click on the title bar it will not maximize the windows form.

How do I stop winform from resizing?

Resizing of the form can be disabled by setting the FormBorderStyle property of the form to `FixedDialog`, `FixedSingle`, or `Fixed3D`.

How do I turn off Windows close button?

We can hide close button on form by setting this. ControlBox=false; Note that this hides all of those sizing buttons.

What button is minimize?

In all versions of Windows, you can minimize windows only using the keyboard by pressing Alt + spacebar . And in the window properties drop-down menu, press N to minimize the window.


1 Answers

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.

like image 61
Rob Avatar answered Oct 16 '22 14:10

Rob