每次按下按鍵激活按鈕時,我都會聽到系統發出“deeng”的聲音。
如何在程式中關閉此聲音?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# form specs
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Text = "Check Network Status"
$Form1.Size = New-Object System.Drawing.Size(300,170)
$Form1.StartPosition = "CenterScreen"
$Form1.KeyPreview = $True
$Form1.MaximumSize = $Form1.Size
$Form1.MinimumSize = $Form1.Size
# form icon
$Form1.Icon = New-Object system.drawing.icon ("C:\Scripts\ps\ADDONS\ico1.ico")
$BackgorundImage = [system.drawing.image]::FromFile("C:\Scripts\ps\ADDONS\jpg1.jpg")
$Form1.BackgroundImage = $BackgorundImage
$Form1.BackgroundImageLayout = "None"
# None, Tile, Center, Stretch, Zoom
$Form1.Width = $BackgorundImage.Width
$Form1.Height = $BackgorundImage.Height
# label
$Label1 = New-Object System.Windows.Forms.label
$Label1.Location = New-Object System.Drawing.Size(7,10)
$Label1.Size = New-Object System.Drawing.Size(130,15)
$Label1.BackColor = "Transparent"
$Label1.ForeColor = "yellow"
$Label1.Text = "Enter Computer Name"
$Form1.Controls.Add($Label1)
# input box
$objTextbox = New-Object System.Windows.Forms.TextBox
$objTextbox.Location = New-Object System.Drawing.Size(10,45)
$objTextbox.Size = New-Object System.Drawing.Size(120,20)
$Form1.Controls.Add($objTextbox)
# ok button
$objButton = New-Object System.Windows.Forms.Button
$objButton.Location = New-Object System.Drawing.Size(140,44)
$objButton.Size = New-Object System.Drawing.Size(75,23)
$objButton.Text = "OK"
$objButton.Add_Click($button_click)
$Form1.Controls.Add($objButton)
# return status
$returnStatus = New-Object System.Windows.Forms.label
$returnStatus.Location = New-Object System.Drawing.Size(8,70)
$returnStatus.Size = New-Object System.Drawing.Size(130,30)
$returnStatus.BackColor = "Transparent"
$returnStatus.Text = ""
$Form1.Controls.Add($returnStatus)
# action item here - you could add your own actions
$button_click =
{
$returnStatus.Text = ""
$computerName = $objTextbox.Text
# output - online
if (Test-Connection $computerName -quiet -Count 1){
Write-Host -ForegroundColor Green "$computerName is online"
$returnStatus.BackColor = "Transparent"
$returnStatus.ForeColor = "lime"
$returnStatus.Text = "Status: Online"
}
Else{
# output - offline
Write-Host -ForegroundColor Red "$computerName is offline"
$returnStatus.ForeColor= "Red"
$returnStatus.Text = "Status: Offline"
}
}
$Form1.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $button_click}})
$Form1.Add_KeyDown({if (($_.Control) -and ($_.KeyCode -eq 'A')){$objTextbox.SelectAll()}})
$Form1.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form1.Close()}})
# modal
$Form1.Topmost = $True
$Form1.Add_Shown({$Form1.Activate()})
[void] $Form1.ShowDialog()
我嘗試過這個:
$_.Handled = $true
$_.SuppressKeyPress = $true
是的,它有助于消除聲音,但如果這些命令存在于代碼中,那么我將無法在 TextBox 中寫入任何內容。
然而,如果有人知道如何制作它,以便當我按下“CTRL A”時,不僅選擇了“SelectAll”命令附加到的文本框,而且我現在所在的文本框也被選中。
我將非常感謝您的幫助!最好的問候, Ar1kato
uj5u.com熱心網友回復:
您需要切換 $_.SuppressKeyPress,例如如下:
$Form1.Add_KeyDown({
$_.SuppressKeyPress = $True
if ($_.KeyCode -eq "Enter"){& $button_click}
elseif (($_.Control) -and ($_.KeyCode -eq 'A')){$objTextbox.SelectAll()}
elseif ($_.KeyCode -eq "Escape"){$Form1.Close()}
else {$_.SuppressKeyPress = $False}
})
請注意,我定義了唯一的Add_KeyDown事件偵聽器……
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389786.html
下一篇:WinForm第一次未加載
