winforms - 如何重新启用Windows窗体中的关闭按钮?

标签 winforms c++-cli

我使用以下代码禁用了表单的关闭按钮:

virtual property System::Windows::Forms::CreateParams^ CreateParams
{
    System::Windows::Forms::CreateParams^ get() override
    {
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle |= 0x200; //CP_NOCLOSE_BUTTON
        return cp;
    }
}

但是,我想在例如 foo() 函数中重新启用此关闭按钮。我该怎么办?

最佳答案

您需要使用 SetClassLong 更改窗口的类样式

中有示例,但想法还是一样的:

public partial class Form1 : Form
{
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            //cp.ClassStyle |= 0x200; // note this is off
            return cp;
        }
    }

    public Form1()
    {
        InitializeComponent();
    }

    // here button is being disabled
    private void Form1_Load(object sender, EventArgs e)
    {
        HandleRef handle = new HandleRef(null, this.Handle);
        var cp = CreateParams;
        cp.ClassStyle = cp.ClassStyle | (0x200);

        IntPtr style = new IntPtr(cp.ClassStyle);
        var classLong = Form1.SetClassLong(handle, (int)ClassLongFlags.GCL_STYLE, style);
    }

    // here is being enabled
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        HandleRef handle = new HandleRef(null, this.Handle);
        var cp = CreateParams;
        cp.ClassStyle = cp.ClassStyle & (~0x200);

        IntPtr style = new IntPtr(cp.ClassStyle);
        var classLong = Form1.SetClassLong(handle, (int)ClassLongFlags.GCL_STYLE, style);
    }
}

SetClassLongClassLongFlags 可以在这里找到 http://www.pinvoke.net/

这里是版本,没有 pinvoke。

#include <windows.h>

#define GCL_STYLE -26
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
using namespace System;

public ref class Form1 : public Form
{
public:
    Form1()
    {
        InitializeComponent();

        this->Load += gcnew EventHandler(
            this, &Form1::Form1_Load);
        this->DoubleClick += gcnew EventHandler(
            this, &Form1::Form1_DoubleClick);
    }
protected:
    virtual property System::Windows::Forms::CreateParams^ CreateParams
    {
        System::Windows::Forms::CreateParams^ get() override
        {
            System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
            //cp->ClassStyle |= 0x200; //CP_NOCLOSE_BUTTON
            return cp;
        }
    }
private:
    void Form1_Load(Object^ sender, EventArgs^ e)
    {
        HandleRef^ handle = gcnew HandleRef(nullptr, this->Handle);
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle = cp->ClassStyle | (0x200);

        IntPtr^ style = gcnew IntPtr(cp->ClassStyle);
        ::SetClassLong(
            (HWND)this->Handle.ToPointer(), 
        (int)GCL_STYLE, 
        (LONG)style->ToInt32());
    }

    // here is being enabled
    // possibly, it is gonna be your `foo`
    void Form1_DoubleClick(Object^ sender, EventArgs^ e)
    {
        HandleRef^ handle = gcnew HandleRef(nullptr, this->Handle);
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle = cp->ClassStyle & (~0x200);

        IntPtr^ style = gcnew IntPtr(cp->ClassStyle);

        ::SetClassLong(
            (HWND)this->Handle.ToPointer(), 
        (int)GCL_STYLE, 
        (LONG)style->ToInt32());
    }
};

关于winforms - 如何重新启用Windows窗体中的关闭按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261182/

相关文章:

c# - 如何设计炫酷的半透明闪屏?

c# - DataGridView Windows 窗体

c# - 开始使用 MVC 创建大型 C# 应用程序的最佳方法是什么?

c++-cli - 使用 C++/CLI 捕获非托管类的返回类型

visual-c++ - 将 native c++ 类导入 CLI 项目

.net - 在托管 c++/.net 4.0 中创建对象的 ConcurrentQueue

.net - 在 C++/CLI 中

c# - 无法将必备组件放在与我的应用程序相同的位置

c# - c#中的无效参数错误

C++/CLI Visual C++ 2010 Express - 绘制多个椭圆