我在 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.
uj5u.com熱心網友回復:
您需要子類化并擴展現有Form類以使覆寫生效:
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而不是 aForm并且生成的表單的關閉按鈕將被禁用:
$root = [FormWithoutX]::new()
# ...
uj5u.com熱心網友回復:
我張貼點小文章@Mathias R.杰森的回答上面,以防它可以幫助別人。我已經包裹在一個自定義的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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/347993.html
下一篇:為什么我收到例外System.Reflection.TargetInvocationException:'Exceptionhasbeenthrowedbythetargetofaninvo
