powershell class 表单按钮的构造函数

powershell class constructor for form buttons

我有一个 powershell 表单,用于管理已安装的应用程序并提供快速卸载该应用程序的方法。我想要做的是为表单上的按钮创建一个 class 构造函数,这样我就可以从另一个函数启用或禁用表单上按钮的状态。以下是我正在尝试做的事情,但我愿意接受其他建议。

Add-Type -AssemblyName System.Windows.Forms

class App
{
    [string] $Name
    [bool] $Installed
    [string] $Key32
    [string] $Key64
    [string] $Inst_Dir
    [bool] $Current
    [string] $Exec
    [string] $User_Ver
    [string] $Inst_Ver

    App ([string] $Name, [bool] $Installed, [string] $Key32, [string] $Key64, [string] $Inst_Dir, [bool] $Current, [string] $Exec, [string] $User_Ver, [string] $Inst_Ver)
    {
        $this.Name = $Name
        $this.Installed = $Installed
        $this.Key32 = $Key32
        $this.Key64 = $Key64
        $this.Inst_Dir = $Inst_Dir
        $this.current = $Current
        $this.Exec = $Exec
        $this.User_Ver = $User_Ver
        $this.Inst_Ver = $Inst_Ver
    }
}

class Button
{    
    [string] $Text
    [int] $Width
    [int] $Height
    [string] $Font
    [bool] $Enabled

    Button ([string] $Text, [int] $Width, [int] $Height, [string] $Font, [bool] $Enabled)
    {
        $this.Text = $Text
        $this.Width = $Width
        $this.Height = $Height
        $this.Font = $Font
        $this.Enabled = $Enabled
    }
}

#region Define and initialize Variables
$HKLM32 = ".\SOFTWARE\Key Location"
$HKLM64 = ".\SOFTWARE\Wow6432Node\Key Location"

# Initialize app classes
[App] $TestApp= [App]::new("TestApp", $false, "$HKLM32\TestApp $TestApp_Ver", "$HKLM64\TestApp $TestApp_Ver", "", $false, "", $TestApp_Ver, "")

$apps = $TestApp

Function create_form()
{
    $Form = New-Object system.Windows.Forms.Form 
    $Form.Text = "Performance Test Auto Setup"
    $Form.TopMost = $true
    $Form.Width = 480
    $Form.Height = 300
    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = "Fixed3D"
    $Form.MaximizeBox = $false
    $Form.MinimizeBox = $false

    [Button] $btnTestAppUninstall = [Button]::new("Uninstall", 95, 22, "Microsoft Sans Serif, 10", $true)
    New-Object System.Drawing.Point(285, 9)

    <#
    $btnTestAppUninstall = New-Object system.windows.Forms.Button 
    $btnTestAppUninstall.Text = "Uninstall"
    $btnTestAppUninstall.Width = 95
    $btnTestAppUninstall.Height = 22
    $btnTestAppUninstall.location = new-object system.drawing.point(285,9)
    $btnTestAppUninstall.Font = "Microsoft Sans Serif,10"
    $btnTestAppUninstall.Enabled = $false
    $Form.controls.Add($btnTestAppUninstall) 
    $btnTestAppUninstall.Add_Click({uninstallApp($TestApp.Inst_Dir)})
    #>

    $btnTest = New-Object system.windows.Forms.Button 
    $btnTest.Text = "test"
    $btnTest.Width = 95
    $btnTest.Height = 22
    $btnTest.location = new-object system.drawing.point(50,9)
    $btnTest.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($btnTest) 
    $btnTest.Add_Click({updateUI})


    $Form.ShowDialog() | Out-Null
}

Function updateUI()
{
    $apps | ForEach-Object {$app = [string]("$"+"btn$($_.Name)Uninstall")}
    Write-Host  $app.GetType() $btnTestAppUninstall.GetType()
}


create_Form 

创建表单中间的注释部分是我最初在创建表单时创建按钮的方式,效果很好。问题是从另一个函数调整该按钮的 button.Enabled 状态,该函数在 $apps 数组上使用 foreach-object 循环。我已经将这个片段精简为表单上的一个按钮,启动时将显示一个测试按钮,但没有其他内容,因为 class 构造函数不会创建按钮。测试按钮功能 "updateUI" 是我尝试这样做的原因,因为在创建按钮变量名称时,我创建的方式是创建 "string" 而不是 "system.drawings.button" 对象,因此我无法访问已启用 属性.

我希望我在这里所做的事情是清楚的,并且可以回答任何问题。对于我在代码上草率的一半工作,我深表歉意,所以这是我开始处理 class 构造函数之前的代码片段,我有一个按钮但无法更改状态。

Add-Type -AssemblyName System.Windows.Forms

class App
{
    [string] $Name
    [bool] $Installed
    [string] $Key32
    [string] $Key64
    [string] $Inst_Dir
    [bool] $Current
    [string] $Exec
    [string] $User_Ver
    [string] $Inst_Ver

    App ([string] $Name, [bool] $Installed, [string] $Key32, [string] $Key64, [string] $Inst_Dir, [bool] $Current, [string] $Exec, [string] $User_Ver, [string] $Inst_Ver)
    {
        $this.Name = $Name
        $this.Installed = $Installed
        $this.Key32 = $Key32
        $this.Key64 = $Key64
        $this.Inst_Dir = $Inst_Dir
        $this.current = $Current
        $this.Exec = $Exec
        $this.User_Ver = $User_Ver
        $this.Inst_Ver = $Inst_Ver
    }
}

#region Define and initialize Variables
$HKLM32 = ".\SOFTWARE\Key Location"
$HKLM64 = ".\SOFTWARE\Wow6432Node\Key Location"

# Initialize app classes
[App] $TestApp = [App]::new("TestApp", $false, "$HKLM32\TestApp $TestApp_Ver", "$HKLM64\TestApp $TestApp_Ver", "", $false, "", $TestApp_Ver, "")

$apps = $TestApp

Function create_form()
{
    $Form = New-Object system.Windows.Forms.Form 
    $Form.Text = "Performance Test Auto Setup"
    $Form.TopMost = $true
    $Form.Width = 480
    $Form.Height = 300
    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = "Fixed3D"
    $Form.MaximizeBox = $false
    $Form.MinimizeBox = $false

    $btnTestAppUninstall = New-Object system.windows.Forms.Button 
    $btnTestAppUninstall.Text = "Uninstall"
    $btnTestAppUninstall.Width = 95
    $btnTestAppUninstall.Height = 22
    $btnTestAppUninstall.location = new-object system.drawing.point(285,9)
    $btnTestAppUninstall.Font = "Microsoft Sans Serif,10"
    $btnTestAppUninstall.Enabled = $false
    $Form.controls.Add($btnTestAppUninstall) 
    $btnTestAppUninstall.Add_Click({uninstallApp($TestApp.Inst_Dir)})

    $btnTest = New-Object system.windows.Forms.Button 
    $btnTest.Text = "test"
    $btnTest.Width = 95
    $btnTest.Height = 22
    $btnTest.location = new-object system.drawing.point(50,9)
    $btnTest.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($btnTest) 
    $btnTest.Add_Click({updateUI})


    $Form.ShowDialog() | Out-Null
}

Function updateUI()
{
    $apps | ForEach-Object {$app = [string]("$"+"btn$($_.Name)Uninstall")}
    Write-Host  $app.GetType() $btnTestAppUninstall.GetType()
}


create_Form 

另外值得注意的是,我知道我可以直接使用

调整状态
$btnTestAppUninstall.Enabled = $true

$btnTestAppUninstalled.Enabled = $false

但这不是最佳解决方案,因为我尝试解决此问题的原因是,在整个 updateUI 函数中,表单上的每个按钮都必须在多个位置具有这两个选项,而真实表单上有 22 个按钮.那里有很多 DRY 被破坏了。如果我能完成这项工作,我可以简单地使用 foreach-object 循环遍历所有内容,并根据 class 对象中存储的值设置状态。

感谢任何帮助我已经尝试解决这个问题好几天了。

所以我最终放弃了这种方法并使用了一个很好的旧 for 循环。我所做的是根据表单中启用的内容创建动态列表,然后我使用索引号同时循环列表以将适当的值放入适当的对象。

Function updateVersions()
{   
    for($i = 0; $i -lt $apps.Length; $i++)
    {
        If(prop32Bit($apps[$i].Key32) -Not $Null)
        {
            $apps[$i].Inst_Dir = $appDir
            $apps[$i].Exec = (Get-ItemProperty -Path $apps[$i].Key32)."Application Name"
            $apps[$i].Ver = (Get-ItemProperty -Path $apps[$i].Key32).Version
            $apps[$i].Installed = $true
        }
        ElseIf (prop64Bit($apps[$i].Key64) -Not $Null)
        {
            $apps[$i].Inst_Dir = $appDir
            $apps[$i].Exec = (Get-ItemProperty -Path $apps[$i].Key64)."Application Name"
            $apps[$i].Inst_Ver = (Get-ItemProperty -Path $apps[$i].Key64).Version
            $apps[$i].Installed = $true
        }
        Else
        {
            $apps[$i].Inst_Dir = $Null
            $apps[$i].Exec = $Null
            $apps[$i].Inst_Ver = "Not Installed"
            $apps[$i].Installed = $false
        }
    }

    # Update version labels
    For($i = 0; $i -lt $listVerLbls.Length; $i++)
    {
        $listVerLbls[$i].Text = $apps[$i].Inst_Ver        
    }

    # Update uninstall buttons
    For($i = 0; $i -lt $listUninBtns.Length; $i++)
    {
        If($apps[$i].installed)
        {
            $listUninBtns[$i].Enabled = $true
        }
        Else
        {
            $listUninBtns[$i].Enabled = $false
        }
    }

    # Update current buttons
    For($i = 0; $i -lt $listCurBtns.Length; $i++)
    {
        If($apps[$i].installed)
        {
            $listCurBtns[$i].Enabled = $true
            $listCurBtns[$i].BackColor = "DarkRed"
            $listCurBtns[$i].ForeColor = "White"
        }
        Else
        {
            $listCurBtns[$i].Enabled = $false
            $listCurBtns[$i].BackColor = "Control"
            $listCurBtns[$i].ForeColor = "ControlDark"
        }
    }
}

这非常适合我想要做的事情。