问题
Is there a way to bring a PoweR Shell popup to the front of the screen? i use this command to show the popup
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("text" ,0,"header",0+64)
but when i use form and a button in the form bis supposed to bring up the popup it shows in the back behind the form the form itself opens in the center and not bringing to front as shows here
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1370,720'
$Form.text = "Chame Wizard"
$Form.TopMost = $true
$Form.icon = "c:\script\chame.ico"
$FormImage = [system.drawing.image]::FromFile("c:\script\back2.jpg")
$Form.BackgroundImage = $FormImage
$Form.StartPosition = "CenterScreen"
i know i can use balloon popup but i want the user to press OK before the script continues. Thanks :-)
回答1:
You can also use one of the overloaded methods of [System.Windows.Forms.MessageBox]::Show()
which allows you to add the owner window in order to have the messagebox be topmost to that.
By using $null
there, your messagebox will be topmost to all opened windows:
function Show-MessageBox {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $false)]
[string]$Title = 'MessageBox in PowerShell',
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $false)]
[ValidateSet('OK', 'OKCancel', 'AbortRetryIgnore', 'YesNoCancel', 'YesNo', 'RetryCancel')]
[string]$Buttons = 'OKCancel',
[Parameter(Mandatory = $false)]
[ValidateSet('Error', 'Warning', 'Information', 'None', 'Question')]
[string]$Icon = 'Information',
[Parameter(Mandatory = $false)]
[ValidateRange(1,3)]
[int]$DefaultButton = 1
)
# determine the possible default button
if ($Buttons -eq 'OK') {
$Default = 'Button1'
}
elseif (@('AbortRetryIgnore', 'YesNoCancel') -contains $Buttons) {
$Default = 'Button{0}' -f [math]::Max([math]::Min($DefaultButton, 3), 1)
}
else {
$Default = 'Button{0}' -f [math]::Max([math]::Min($DefaultButton, 2), 1)
}
Add-Type -AssemblyName System.Windows.Forms
# Setting the first parameter 'owner' to $null lets he messagebox become topmost
[System.Windows.Forms.MessageBox]::Show($null, $Message, $Title,
[Windows.Forms.MessageBoxButtons]::$Buttons,
[Windows.Forms.MessageBoxIcon]::$Icon,
[Windows.Forms.MessageBoxDefaultButton]::$Default)
}
With this function in place, you call it like:
Show-MessageBox -Title 'Important message' -Message 'Hi there!' -Icon Information -Buttons OK
来源:https://stackoverflow.com/questions/59371640/powershell-popup-in-form