表单中的PowerShell弹出窗口 [英] PowerShell popup in form

查看:161
本文介绍了表单中的PowerShell弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将PoweR Shell弹出窗口带到屏幕的前面? 我使用此命令显示弹出窗口

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)

但是当我使用窗体和窗体bis中的按钮应该弹出菜单时,它显示在窗体后面 表单本身会在中心打开,而不是显示在此处的前面

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"

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

i know i can use balloon popup but i want the user to press OK before the script continues. Thanks :-)

推荐答案

您还可以使用[System.Windows.Forms.MessageBox]::Show()的重载方法之一,该方法允许您添加所有者窗口,以使消息框位于顶部.

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.

通过在此处使用$null,您的消息框将位于所有打开的窗口的最上方:

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

这篇关于表单中的PowerShell弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆