c# - Windows 窗体可以在没有关闭按钮的情况下显示最小和最大按钮吗?

标签 c# .net windows

是否有任何方法(在 C# 中)显示仅包含最小化和最大化按钮的表单?没有关闭按钮?

删除关闭按钮(据我所知)的唯一方法是:

form.ControlBox = false;

但这也摆脱了其他两个按钮。

最佳答案

我编写了一个函数来执行此操作一次

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        if (EnableMenuItem(GetSystemMenu(this.Handle, 0), SC_CLOSE, MF_GRAYED) == -1)
            throw new Win32Exception("The message box did not exist to gray out its X");
    }
    private const int SC_CLOSE = 0xF060;
    private const int MF_GRAYED = 0x1;
    [DllImport("USER32")]
    internal static extern int EnableMenuItem(IntPtr WindowHandle, int uIDEnableItem, int uEnable);
    [DllImport("USER32")]
    internal static extern IntPtr GetSystemMenu(IntPtr WindowHandle, int bReset);
}

请注意,当您从任务栏查看它时,alt-f4 仍然有效,并右键单击“关闭此窗口”。 (在Windows 7中测试)

关于c# - Windows 窗体可以在没有关闭按钮的情况下显示最小和最大按钮吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3113539/

相关文章:

windows - Windows 上的 flutter : how to call an external dll?

windows - 在 Windows 机器上运行 Ansible 时出现 "Undefined variable"错误

c# - 无法确定条件表达式的类型,因为 'string' 和 'System.DBNull' 之间没有隐式转换

c# - 获取图像文件夹的路径

c# - 从另一个 AppDomain 编码程序集

.net - 无法调用 Dispose?

c# - ReadToEndAsync 是阻塞的,如何让它异步运行?

windows - Windows 8.1 更新后出现 Vagrant 错误

c# - RabbitMQ CreateConnection 问题 - 在一个应用程序中有效但在另一个应用程序中无效

c# - 为什么 C# 8 数组切片功能中的新帽子运算符索引不从 0 开始?