gpt4 book ai didi

c# - 如何禁用C#中的最小化按钮?

转载 作者:太空狗 更新时间:2023-10-29 20:47:14 25 4
gpt4 key购买 nike

在我的应用程序中,我需要暂时使主窗体的最小化按钮变灰。有什么想法可以实现吗?我不介意对 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 article about it .

关于c# - 如何禁用C#中的最小化按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/319124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com