首页 > 解决方案 > 在 PowerShell 中喜欢蓝色消息框?

问题描述

有没有办法从 PowerShell 中创建那些漂亮的、带有灰色背景的漂亮蓝色消息框,就像 Windows 更新使用的那样?像这样?

提前致谢!V·施泰格

标签: powershelluser-interface

解决方案


您可以通过创建几个相关的窗口窗体并正确设置它们的样式来模拟这一点。这是一个样式非常粗略的示例。

注意:ALT+F4 将退出表单。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles() 

$backgroundForm = New-Object system.Windows.Forms.Form 
$backgroundForm.StartPosition = "CenterScreen" 


$backgroundForm.WindowState = [System.Windows.Forms.FormWindowState]::Maximized
$backgroundForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::SizableToolWindow
$backgroundForm.ControlBox = $false
$backgroundForm.ShowInTaskbar = $False
$backgroundForm.BackColor = [System.Drawing.Color]::Black
$backgroundForm.Opacity = .5
$backgroundForm.Text = ''
$backgroundForm.TopMost = $true



$backgroundForm.add_Shown({
    param([object]$sender, [System.EventArgs]$e)
    
    $msgForm = New-Object system.Windows.Forms.Form 
    $msgForm.StartPosition = "CenterScreen" 


    $msgForm.Width = 400
    $msgForm.Height = 250
    $msgForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
    $msgForm.ControlBox = $false
    $msgForm.ShowInTaskbar = $False
    $msgForm.BackColor = [System.Drawing.Color]::Blue
    $msgForm.ForeColor = [System.Drawing.Color]::White
    $msgForm.Text = ''
    $msgForm.Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold) 
    
    $label = New-Object System.Windows.Forms.Label 
    $label.Text = "You'll need to style as appropriate" 
    $label.AutoSize = $true 
    $label.Location = New-Object System.Drawing.Size(10,50) 

    $button = New-Object System.Windows.Forms.Label 
    $button.Text = 'Close'
    $button.AutoSize = $true
    $button.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $button.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $button.Location = New-Object System.Drawing.Size(300,200) 
    

    $button.add_Click({
        param([object]$sender, [System.EventArgs]$e)

        $sender.Parent.Close()
    })


    $msgForm.Controls.Add($Label) 
    $msgForm.Controls.Add($button) 

    $msgForm.Modal = $true
    
    $msgForm.ShowDialog($sender)

    $sender.Close()
    
})

$backgroundForm.ShowDialog()

推荐阅读