如何使用 Add-Type C# 代码禁用 PowerShell Windows 表单中的关闭按钮?
How to disable close button in PowerShell Windows Forms using Add-Type C# code?
我在 PowerShell 中有一个 Windows 表单,我试图在其中禁用关闭按钮 X
。我找到了一些用于执行此操作的 C# 代码 (Source 1, Source 2),但我无法使其与 PowerShell 5.1 一起使用。
这是我的代码:
$codeDisableX = @"
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
"@
Add-Type -TypeDefinition $codeDisableX -ReferencedAssemblies System.Windows.Forms -Language CSharp
Add-Type -AssemblyName System.Windows.Forms
$root = [System.Windows.Forms.Form]::new()
# Remaining code for Windows Forms not included
有人知道我如何使用 PowerShell 实现 C# 代码来禁用关闭按钮吗?
还有,我不想用$root.ControlBox = $false
。
您需要对现有的 Form
class 进行子class 和 扩展 才能使覆盖生效:
Add-Type -AssemblyName System.Windows.Forms
Add-Type @'
using System.Windows.Forms;
public class FormWithoutX : Form {
protected override CreateParams CreateParams
{
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | 0x200;
return cp;
}
}
}
'@ -ReferencedAssemblies System.Windows.Forms
现在您可以创建 FormWithoutX
而不是 Form
,生成的表单的关闭按钮将被禁用:
$root = [FormWithoutX]::new()
# ...
我正在对 @Mathias R. Jessen's answer 进行微调,以防对其他人有所帮助。我已将 C# 代码包装在自定义 namespace
中,并使用 [MyForm.FormWithoutX]::new()
创建了一个新表单。这允许将 C# 代码保存在创建表单时使用的变量中:
$codeDisableX = @"
using System.Windows.Forms;
namespace MyForm {
public class FormWithoutX : Form {
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | 0x200;
return cp;
}
}
}
}
"@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -TypeDefinition $codeDisableButtonX -ReferencedAssemblies System.Windows.Forms
$root = [MyForm.FormWithoutX]::new()
# Remaining code for Windows Forms not included
我在 PowerShell 中有一个 Windows 表单,我试图在其中禁用关闭按钮 X
。我找到了一些用于执行此操作的 C# 代码 (Source 1, Source 2),但我无法使其与 PowerShell 5.1 一起使用。
这是我的代码:
$codeDisableX = @"
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
"@
Add-Type -TypeDefinition $codeDisableX -ReferencedAssemblies System.Windows.Forms -Language CSharp
Add-Type -AssemblyName System.Windows.Forms
$root = [System.Windows.Forms.Form]::new()
# Remaining code for Windows Forms not included
有人知道我如何使用 PowerShell 实现 C# 代码来禁用关闭按钮吗?
还有,我不想用$root.ControlBox = $false
。
您需要对现有的 Form
class 进行子class 和 扩展 才能使覆盖生效:
Add-Type -AssemblyName System.Windows.Forms
Add-Type @'
using System.Windows.Forms;
public class FormWithoutX : Form {
protected override CreateParams CreateParams
{
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | 0x200;
return cp;
}
}
}
'@ -ReferencedAssemblies System.Windows.Forms
现在您可以创建 FormWithoutX
而不是 Form
,生成的表单的关闭按钮将被禁用:
$root = [FormWithoutX]::new()
# ...
我正在对 @Mathias R. Jessen's answer namespace
中,并使用 [MyForm.FormWithoutX]::new()
创建了一个新表单。这允许将 C# 代码保存在创建表单时使用的变量中:
$codeDisableX = @"
using System.Windows.Forms;
namespace MyForm {
public class FormWithoutX : Form {
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | 0x200;
return cp;
}
}
}
}
"@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -TypeDefinition $codeDisableButtonX -ReferencedAssemblies System.Windows.Forms
$root = [MyForm.FormWithoutX]::new()
# Remaining code for Windows Forms not included