我使用以下代码禁用了表单的关闭按钮:
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()函数中重新启用此关闭按钮。我该怎么办?
答案 0 :(得分:4)
您需要使用SetClassLong
c#中有例子,但想法仍然相同:
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
可以在此处找到http://www.pinvoke.net/
这是c++-cli版本,没有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());
}
};