gpt4 book ai didi

forms - 窗体中的 PowerShell 弹出窗口

转载 作者:行者123 更新时间:2023-12-04 10:53:07 26 4
gpt4 key购买 nike

有没有办法将 Power Shell 弹出窗口带到屏幕前面?
我使用这个命令来显示弹出窗口


$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("text" ,0,"header",0+64)


但是当我使用表单和表单 bis 中的按钮时,它应该弹出它显示在表单后面的弹出窗口
表格本身在中心打开,而不是像这里显示的那样放在前面
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"

我知道我可以使用气球弹出窗口,但我希望用户在脚本继续之前按 OK。
谢谢 :-)

最佳答案

您还可以使用 [System.Windows.Forms.MessageBox]::Show() 的重载方法之一。它允许您添加所有者窗口,以使消息框位于最上面。
通过使用 $null在那里,您的消息框将位于所有打开的窗口的最顶部:

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
# added from tip by [Ste](https://stackoverflow.com/users/8262102/ste) so the
# button gets highlighted when the mouse hovers over it.
[void][System.Windows.Forms.Application]::EnableVisualStyles()

# 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)
}
有了这个函数,你可以这样称呼它:
Show-MessageBox -Title 'Important message' -Message 'Hi there!' -Icon Information -Buttons OK
enter image description here

编辑
正如 Ste 所问,以上函数显示消息框 TopMost .然而,这并不意味着它是 Modal .这仅意味着该框在第一次显示时显示在顶部,但可以通过激活其他窗口将其推到背景中。
对于无法推送到后台的真实 Modal 消息框,我使用:
function Show-MessageBox {
[CmdletBinding()]
param(
[parameter(Mandatory = $true, Position = 0)]
[string]$Message,

[parameter(Mandatory = $false)]
[string]$Title = 'MessageBox in PowerShell',

[ValidateSet("OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel")]
[string]$Buttons = "OKCancel",

[ValidateSet("Critical", "Question", "Exclamation", "Information")]
[string]$Icon = "Information"
)
Add-Type -AssemblyName Microsoft.VisualBasic

[Microsoft.VisualBasic.Interaction]::MsgBox($Message, "$Buttons,SystemModal,$Icon", $Title)
}


Show-MessageBox -Title 'Important message' -Message 'Hi there!' -Icon Information -Buttons OKOnly
enter image description here

关于forms - 窗体中的 PowerShell 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59371640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com