gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:26:33 31 4
gpt4 key购买 nike

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

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/

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