首先,我對 PowerShell 和 C# 非常陌生,目前正在嘗試學習它。我正在使用 System.Windows.Forms 類創建表單,并制作了一個包含兩個按鈕和一個復選框的小視窗。其中一個按鈕將保持隱藏狀態,而另一個按鈕可以看到以進行某種交換操作,因為我沒有想出另一種方法來做到這一點。第一個按鈕激活,如果復選框為 $true,則它觸發 shutdown.exe /s 行程并更改第二個按鈕的變數。我讓它使第一個按鈕正常作業,但第二個按鈕似乎根本沒有做任何事情。你介意檢查我的代碼告訴我哪里出錯了嗎?
PowerShell.exe -windowstyle hidden {
# Start of code
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Declaration of the form
$folderForm = New-Object System.Windows.Forms.Form
$folderForm.StartPosition = 'Manual'
$folderForm.Location = '2267,1107'
# Declaration of the Check Box
$shutdownCheck = New-Object System.Windows.Forms.CheckBox
$shutdownCheck.Location = New-Object System.Drawing.Size(23,23)
$shutdownCheck.Size = '27,27'
$folderForm.Controls.Add($shutdownCheck)
# Declaration of the Shutdown-Button
$shutdownButton = New-Object System.Windows.Forms.Button
$shutdownButton.Text = 'Feierabendmodus aktivieren'
$shutdownButton.Location = '120,23'
$shutdownButton.Size = '160,23'
$folderForm.Controls.Add($shutdownButton)
# Declaration of the Stop-Button
$stopButton = New-Object System.Windows.Forms.Button
$stopButton.Text = 'Sequenz abbrechen!'
$stopButton.Location = '120,23'
$stopButton.Size = '160,23'
$stopButton.Visible = $false
$folderForm.Controls.Add($stopButton)
function TestClick
{
$varX = $true
#Start
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Filler','More Filler',
[system.windows.forms.tooltipicon]::None)
# Shutdown-Prozess
Start-Process shutdown.exe /s
#Stop-Process powershell_ise -force
$shutdownButton.Visible = $false
$stopButton.Visible = $true
$shutdownCheck.Checked = $false
$varX = $false
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
# Stop
Start-Process shutdown.exe /a
$shutdownButton.Text = 'Filler'
$stopButton.Visible = $false
$shutdownButton.Visible = $true
$varX = $true
}
}
$shutdownButton.Add_Click({TestClick})
$stopButton.Add_Click({TestClick})
$folderForm.ShowDialog()
# End of code
}
uj5u.com熱心網友回復:
下面的代碼創建了一個按鈕,其文本在單擊后會發生變化 - 這是您的代碼,經過一些修改。請嘗試以下操作,看看它是否滿足您的需求:
PowerShell.exe -windowstyle normal {
# Start of code
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Declaration of the form
$folderForm = New-Object System.Windows.Forms.Form
$folderForm.StartPosition = 'Manual'
$folderForm.Location = '267,107'
$folderForm.Size = '315, 200'
$folderForm.Text = 'Shutdown'
# Declaration of the Shutdown-Button
$shutdownButton = New-Object System.Windows.Forms.Button
$shutdownButton.Text = 'Shutdown'
$shutdownButton.Location = '75,23'
$shutdownButton.Size = '150,23'
$folderForm.Controls.Add($shutdownButton)
function TestClick
{
#Start
if ($shutdownButton.Text -eq 'Shutdown')
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Shutdown','Shutting down the computer.',
[system.windows.forms.tooltipicon]::None)
#ToDo: replace the code below with desired code
# Shutdown-Prozess
$shutdownButton.Text = 'Abort Shutdown'
#Start-Process shutdown.exe /t 60
Start-Process shutdown.exe /?
}
else
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Shutdown Aborted','Shutdown has been aborted.',
[system.windows.forms.tooltipicon]::None)
# Stop
$shutdownButton.Text = 'Shutdown'
Start-Process shutdown.exe /a
}
}
$shutdownButton.Add_Click({TestClick})
$folderForm.ShowDialog()
# End of code
}
uj5u.com熱心網友回復:
兩個問題:
$varX每次單擊時都設定為 true- TestClick 函式僅更新本地范圍
$varX(僅在該函式內)
# your current code (shortened)
function TestClick
{
$varX = $true ## getting set every button click (in local scope)
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
$varX = $false ## local scope
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
$varX = $true ## local scope
}
}
要修復,請嘗試執行以下操作:
function TestClick
{
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
$Script:varX = $false ## Scope the variable so the function sets it properly
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
$Script:varX = $true ## here too
}
}
$varX = $true ## Move the initial setting outside the function
我建議一起跳過函式和全域狀態,只需使用您創建的不同按鈕:
# shutdown button
$shutdownButton.Add_Click({
if ($shutdownCheck.Checked) {
Start-Process shutdown.exe /s
$shutdownButton.Visible = $false
$shutdownCheck.Checked = $false
$stopButton.Visible = $true
}
})
# stop button (hidden until shutdown clicked)
$stopButton.Add_Click({
Start-Process shutdown.exe /a
$shutdownButton.Visible = $true
$shutdownButton.Text = 'Stopped'
$stopButton.Visible = $false
})
$folderForm.ShowDialog()
# End of code
編輯:在腳本中運行函式會創建一個子范圍,該范圍從腳本級別繼承變數等元素。當您在函式內設定變數(不設定范圍)時,該變數僅在該函式執行時存在于該函式的范圍內,并且不會保留。我認為在實踐中更容易看到:
$A = 'Hello'
$B = 'World'
Function Set-Goodbye {
$B = 'Goodbye'
# The default scope for variables set inside a script
Write-Output "function - local: $A $B"
# Manually check the higher script-level scope
Write-Output "function - script: $A $Script:B"
}
# print output
Set-Goodbye
"script - local: $A $B"
function - local: Hello Goodbye
function - script: Hello World
script - local: Hello World
如需更好的解釋,請查看about_Scopes頁面
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412559.html
標籤:
