如何在C#中禁用最小化按钮?

时间:2022-04-25 07:16:42

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.

在我的应用程序中,我需要暂时灰显主窗体的最小化按钮。任何想法如何实现这一目标?我不介意对Win32 dll进行p /调用。

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

编辑:灰显最小化按钮将是首选解决方案,但有没有其他方法可以防止表单最小化?

8 个解决方案

#1


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:

我阅读了您对我的回复的评论,并为您提供了更完整的解决方案。我跑得很快,似乎有你想要的行为。而不是从Form派生你的winforms,而是派生自这个类:


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.

有关此窗口通知的详细信息,请参阅有关它的MSDN文章。

#2


10  

form.MinimizeBox = false;

or if in the form scope

或者如果在表格范围内

MinimizeBox = false;

#3


1  

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

只做MinimizeBox = false;在你的表单的代码中。

#4


1  

Put this code in your form's Resize event:

将此代码放在表单的Resize事件中:

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).

这将使您的表单不可最小化(免责声明:我不主张以这种方式改变Windows的标准行为)。

#5


0  

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

您还可以实现Minimize事件的句柄以取消该命令

#6


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.

别。别乱我的窗户。他们是我的,不是你的。这是我的电脑,如果我想最小化,我应该能够。我无法想到,也从未给出过这样做的充分理由。

#7


0  

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

Coincoin的答案是正确的。 MinimizeBox也可作为设计器属性窗口中的属性使用。

@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.

@Kevin:虽然我很欣赏这种情绪,但这并不总是一个有效的答案。如果应用程序通过创建Form的新实例然后在其上调用.ShowDialog()来显示模式对话框,则您不希望用户将该Form最小化,因为此时主UI线程上的所有输入都将被阻止,直到Form的模态状态得到满足。用户可能会点击主表单,只是从Windows获得“ding ding ding”无响应的声音而不知道该怎么做。

#8


0  

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

只需将表单的MinimizeBox属性设置为false。这将禁用最小化按钮,但其他按钮将保持功能。

#1


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:

我阅读了您对我的回复的评论,并为您提供了更完整的解决方案。我跑得很快,似乎有你想要的行为。而不是从Form派生你的winforms,而是派生自这个类:


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.

有关此窗口通知的详细信息,请参阅有关它的MSDN文章。

#2


10  

form.MinimizeBox = false;

or if in the form scope

或者如果在表格范围内

MinimizeBox = false;

#3


1  

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

只做MinimizeBox = false;在你的表单的代码中。

#4


1  

Put this code in your form's Resize event:

将此代码放在表单的Resize事件中:

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).

这将使您的表单不可最小化(免责声明:我不主张以这种方式改变Windows的标准行为)。

#5


0  

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

您还可以实现Minimize事件的句柄以取消该命令

#6


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.

别。别乱我的窗户。他们是我的,不是你的。这是我的电脑,如果我想最小化,我应该能够。我无法想到,也从未给出过这样做的充分理由。

#7


0  

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

Coincoin的答案是正确的。 MinimizeBox也可作为设计器属性窗口中的属性使用。

@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.

@Kevin:虽然我很欣赏这种情绪,但这并不总是一个有效的答案。如果应用程序通过创建Form的新实例然后在其上调用.ShowDialog()来显示模式对话框,则您不希望用户将该Form最小化,因为此时主UI线程上的所有输入都将被阻止,直到Form的模态状态得到满足。用户可能会点击主表单,只是从Windows获得“ding ding ding”无响应的声音而不知道该怎么做。

#8


0  

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

只需将表单的MinimizeBox属性设置为false。这将禁用最小化按钮,但其他按钮将保持功能。