簡體   English   中英

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

[英]How to disable the minimize button in C#?

在我的應用程序中,我需要暫時灰顯主窗體的最小化按鈕。 任何想法如何實現這一目標? 我不介意對Win32 dll進行p /調用。

編輯:灰顯最小化按鈕將是首選解決方案,但有沒有其他方法可以防止表單最小化?

我閱讀了您對我的回復的評論,並為您提供了更完整的解決方案。 我跑得很快,似乎有你想要的行為。 而不是從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;
        }
    }
}

如果您希望能夠在發送點擊時決定是否允許最小化,則可以執行更多操作,例如:


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);
        }
    }
}

有關此窗口通知的詳細信息,請參閱有關它MSDN文章

form.MinimizeBox = false;

或者如果在表格范圍內

MinimizeBox = false;

做MinimizeBox = false; 在你的表單的代碼中。

將此代碼放在表單的Resize事件中:

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

這將使您的表單不可最小化(免責聲明:我不主張以這種方式改變Windows的標准行為)。

只需將表單的MinimizeBox屬性設置為false。 這將禁用最小化按鈕,但其他按鈕將保持功能。

您還可以實現Minimize事件的句柄以取消該命令

別。 別亂我的窗戶。 他們是我的,不是你的。 這是我的電腦,如果我想最小化,我應該能夠。 我無法想到,也從來沒有給過這樣做的充分理由。

Coincoin的答案是正確的。 MinimizeBox也可作為設計器屬性窗口中的屬性使用。

@Kevin:雖然我很欣賞這種情緒,但這並不總是一個有效的答案。 如果應用程序通過創建Form的新實例然后在其上調用.ShowDialog()來顯示模式對話框,則您不希望用戶最小化該Form,因為然后主UI線程上的所有輸入都將被阻止,直到Form的模態狀態是滿足的。 用戶可能會點擊主表單,只是從Windows獲取“ding ding ding”無響應的聲音而不知道該怎么做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM