Powershell GUI 切换按钮

问题描述 投票:0回答:2

我想在我的 PowerShell GUI 中有一个按钮,可以在深色模式和浅色模式之间切换。当我单击按钮时,将运行此代码。

信息:程序启动时 $isDark 设置为 0

$func_ToggleDarkMode = {
    
    Write-Host "Current Dark Mode $isDark"

        if($isDark -eq 0) {
            $MainGUI.BackColor = "Gray"
            $isDark = 1

        } elseif($isDark -eq 1) {
            $MainGUI.BackColor = "White"
            $isDark = 0
        
    Write-Host "Dark Mode updated to $isDark"
    
}

第一次按下按钮时,界面会变成灰色。控制台说

Current Dark Mode is 0
Dark Mode updated to 1

但是,如果我再按一次,界面不会再次变白,控制台会显示

Current Dark Mode is 0
Dark Mode updated to 1

希望你能帮助我,谢谢:)

powershell user-interface toggle
2个回答
0
投票

这是一个完全取决于您要更改的事物的状态的版本。如果这从外部状态(例如“红色”)开始,这将追溯到受控状态之一。

$func_ToggleDarkMode = {
    $modes = @{Dark = "Grey"; Light = "White"}

    Write-Host "Prior Mode $($modes.keys |? {$modes[$_] -eq $MainGUI.BackColor} )"

    $MainGUI.BackColor = $(if ($MainGUI.BackColor -eq $modes.Dark) { $modes.Light } else { $modes.Dark})

    Write-Host "New Mode $($modes.keys |? {$modes[$_] -eq $MainGUI.BackColor} )"
}

注意:哈希查找(在计划中)很慢,但它写入主机。如果您要进入完全狂欢模式,请注释掉这些行。


0
投票

<# Simulating a toogle button with three Windows Forms labels. Label1 and label2 are on top of label. Label1 left half and label2 the right. The toogle button is used in an application where it controls if the checked boxes in group of checkboxes evaluates as OR or AND. #> 添加类型-AssemblyName System.Windows.Forms 添加类型 -AssemblyName System.Drawing $Form = 新对象 system.Windows.Forms.Form $Form.ClientSize = "1200,800" $Form.text = "Vinregister" $label1 = 新对象 System.Windows.Forms.Label $label1.Location = 新对象 System.Drawing.Point(310,400) $label1.size = 新对象 System.Drawing.Size(50,20) $label1.BackColor = "浅蓝色" $label1.Visible = $true $label1.Text = "更改" $Form.Controls.Add($label1) $label2 = 新对象 System.Windows.Forms.Label $label2.Location = 新对象 System.Drawing.Point(360,400) $label2.size = 新对象 System.Drawing.Size(50,20) $label2.BackColor = "浅蓝色" $label2.Visible = $false $label2.Text = "更改" $Form.Controls.Add($label2) #$label = 新对象 System.Windows.Forms.Button $label= 新对象 System.Windows.Forms.Label $label.Location = 新对象 System.Drawing.Point(310,400) $label.Size = 新对象 System.Drawing.Size(100,22) $label.BackColor = "绿色" $label.Text =“与或” $Form.Controls.Add($label) $label1.Add_Click({ 写主机“State#1” $label1.Visible = $false $label2.Visible = $true # 为 State#1 添加特殊代码 }) $label2.Ad d_Click({ 写主机“State#2” $label1.Visible = $true $label2.Visible = $false # 为 State#2 添加特殊代码 }) $Form.TopMost = $true $Form.Add_Shown( { $form.Activate() } ) $Form.ShowDialog()

© www.soinside.com 2019 - 2024. All rights reserved.