第一个例子:
这个来自这个网页:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/keeping-msgbox-on-top
当您从 PowerShell 打开 MsgBox 对话框时,对话框窗口有时可能不可见,而是显示在 PowerShell 或 ISE 窗口后面。
要确保 MsgBox 对话框出现在 PowerShell 窗口前面,请尝试以下操作:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('My message', 'YesNo,MsgBoxSetForeground,Information', 'MyTitle')
密钥是选项MsgBoxSetForeground。如果您想知道可以选择哪些其他选项,请将第二个参数替换为无意义的文本,错误消息将列出所有其他选项名称。
一个是SystemModal。如果你使用它而不是MsgBoxSetForeground,那么MsgBox 不仅会出现在前面,它还会留在那里。在用户单击其中一个按钮之前,没有其他窗口可以与对话框重叠。
SystemModal 是将其设置为所有窗口最前面的位。
所以使用:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('My message', 'YesNo,SystemModal,Information', 'MyTitle')
第二个例子:
这是一个目录选择器,取自:
https://powershellone.wordpress.com/2016/05/06/powershell-tricks-open-a-dialog-as-topmost-window/
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'
$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
if ($result -eq [Windows.Forms.DialogResult]::OK){
$FolderBrowser.SelectedPath
}
else {
exit
}