Winforms 如何在windows窗体中重新启用关闭按钮?

Winforms 如何在windows窗体中重新启用关闭按钮?,winforms,c++-cli,c#,Winforms,C++ Cli,C#,我使用以下代码禁用了窗体的关闭按钮: virtual property System::Windows::Forms::CreateParams^ CreateParams { System::Windows::Forms::CreateParams^ get() override { System::Windows::Forms::CreateParams^ cp = Form::CreateParams; cp->ClassStyle |

我使用以下代码禁用了窗体的关闭按钮:

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()函数中重新启用此关闭按钮。如何操作?

您需要使用

在中有一个例子,但想法仍然是一样的:

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);
    }
}
SetClassLong
ClassLongFlags
可以在这里找到

这是一个版本,没有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());
    }
};
#包括
#定义GCL_样式-26
使用命名空间System::Windows::Forms;
使用名称空间System::Runtime::InteropServices;
使用名称空间系统;
公共参考类表单1:公共表单
{
公众:
表格1()
{
初始化组件();
此->加载+=gcneweventhandler(
这,&Form1::Form1_加载);
此->双击+=gcneweventhandler(
这,&Form1::Form1\u双击);
}
受保护的:
虚拟财产系统::Windows::窗体::CreateParams ^CreateParams
{
System::Windows::Forms::CreateParams^get()覆盖
{
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
//cp->ClassStyle |=0x200;//cp_NOCLOSE_按钮
返回cp;
}
}
私人:
void Form1\u加载(对象^sender,事件参数^e)
{
HandleRef^handle=gcnewhandleref(nullptr,this->handle);
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
cp->ClassStyle=cp->ClassStyle |(0x200);
IntPtr^style=gcnewintptr(cp->ClassStyle);
::SetClassLong(
(HWND)此->Handle.ToPointer(),
(int)GCL_风格,
(长)样式->ToInt32();
}
//这里正在启用
//可能是你的“foo”`
void Form1\u双击(对象^sender,事件参数^e)
{
HandleRef^handle=gcnewhandleref(nullptr,this->handle);
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
cp->ClassStyle=cp->ClassStyle&(~0x200);
IntPtr^style=gcnewintptr(cp->ClassStyle);
::SetClassLong(
(HWND)此->Handle.ToPointer(),
(int)GCL_风格,
(长)样式->ToInt32();
}
};

您需要使用

在中有一个例子,但想法仍然是一样的:

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);
    }
}
SetClassLong
ClassLongFlags
可以在这里找到

这是一个版本,没有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());
    }
};
#包括
#定义GCL_样式-26
使用命名空间System::Windows::Forms;
使用名称空间System::Runtime::InteropServices;
使用名称空间系统;
公共参考类表单1:公共表单
{
公众:
表格1()
{
初始化组件();
此->加载+=gcneweventhandler(
这,&Form1::Form1_加载);
此->双击+=gcneweventhandler(
这,&Form1::Form1\u双击);
}
受保护的:
虚拟财产系统::Windows::窗体::CreateParams ^CreateParams
{
System::Windows::Forms::CreateParams^get()覆盖
{
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
//cp->ClassStyle |=0x200;//cp_NOCLOSE_按钮
返回cp;
}
}
私人:
void Form1\u加载(对象^sender,事件参数^e)
{
HandleRef^handle=gcnewhandleref(nullptr,this->handle);
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
cp->ClassStyle=cp->ClassStyle |(0x200);
IntPtr^style=gcnewintptr(cp->ClassStyle);
::SetClassLong(
(HWND)此->Handle.ToPointer(),
(int)GCL_风格,
(长)样式->ToInt32();
}
//这里正在启用
//可能是你的“foo”`
void Form1\u双击(对象^sender,事件参数^e)
{
HandleRef^handle=gcnewhandleref(nullptr,this->handle);
System::Windows::Forms::CreateParams ^cp=Form::CreateParams;
cp->ClassStyle=cp->ClassStyle&(~0x200);
IntPtr^style=gcnewintptr(cp->ClassStyle);
::SetClassLong(
(HWND)此->Handle.ToPointer(),
(int)GCL_风格,
(长)样式->ToInt32();
}
};

<代码>我无法解决C++问题。他向C展示了它。“哦,UZHANT URK我更新了我的POSSI无法解决它的C++。他为C#展示了它。@Oğuzhanürk我已经更新了我的帖子,我不再参与这个项目,也没有项目的源代码。这看起来是一个正确的答案;所以我接受了,我不再做这个项目了,我也没有这个项目的源代码。这看起来是一个正确的答案;所以我接受了。