是否可以在powershell中创建自定义MessageBox.Icon?

问题描述 投票:0回答:1

我还没有找到任何解决我的问题的方法。 我不喜欢的是我只能显示它们提供的 Windows.System.Form::Icons。 最终的解决方案是什么?能够使用本地保存的 .png 文件并将其保存在变量中,例如:

$ICON = -File C:PATH\Image.png
然后在代码中使用 $ICON
$MessageIcon = [MessageBoxImage]::$ICON
这是我到目前为止编写的代码:

choco upgrade all -y --log-File "C:\ProgramData\chocolatey\logs\chocolatey.log"
if ($LASTEXITCODE -eq 3010) {
    New-Item -Path C:\ProgramData\chocolatey\logs\rebootnow.txt -Force
}
else {
    return
}
if (Test-Path -Path C:\ProgramData\chocolatey\logs\rebootnow.txt) {
    try {
        Add-Type -AssemblyName PresentationCore, PresentationFramework
    }
    catch {
        Write-Host "Exit 2 - Beim Anlegen der Libaries für Windows Forms ist ein Fehler aufgetreten."
        exit 2
    }
    $ButtonType = [System.Windows.MessageBoxButton]::YesNo
    $MessageboxTitle = "Information"
    $Messageboxbody = "Es wurde im Hintergrund vom IT Administrator ein Update für alle Firmeninternen Apps durchgeführt. Es ist ein Geräte-Neustart nötig um das Update zu finalisieren. Möchten Sie jetzt einen Geräte-Neustart durchführen?"
    $MessageIcon = [System.Windows.MessageBoxImage]::Information

    try {
        $result = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
    }
    catch {
        Write-Host "Exit 3 - Beim erstellen der Windows Forms aus den verschiedenen Objekten ist ein Fehler aufgetreten."
        exit 3
    }

    if ($result -eq [System.Windows.MessageBoxResult]::Yes) {
        Remove-Item -Path "C:\ProgramData\chocolatey\logs\rebootnow.txt"
        Shutdown.exe /r /t 15
    }
    else {
        return
    }
}
else {
    return
}

代码本身是功能性的,但问题在于设计部分。我希望它显示公司 .png,以便用户从内部 IT 管理中了解它。

编辑:我也许找到了一个可能的解决方案。 WPF消息框 WPF 文档 我会尝试从中做点什么。如果它有效,我将更新显示的代码。

windows powershell forms winforms
1个回答
0
投票

这是我使用的代码片段:

#--- Form Drawing References ---

  $Form_Width       =  650 #950    
  $Form_Height      =  275 #605
  $Btn_Height       =   35
  $tabWidth         = $Form_Width-10
  $cboxHeight       =   30
  $cboxWidth        =  200
  $cboxFontSize     =   18
  $tboxHeight       =   30
  $tboxWidth        =  220
  $StdSpacing       =   35
  $CkBoxSpacing     =   10
  $ExitBtnWidth     =   80
  $ExecuteBtnWidth  =  100
  $StatusWidth      =  $Form_Width - 
                       ($ExitBtnWidth+$ExecuteBtnWidth+45)

$IconFile = "$($PSScriptRoot)\ComputerMentor.ico"        
If (-not (Test-Path -Path "$IconFile")){
  $Message = "Error Icon file not found:`n" +
              "$IconFile`n" + 
              "Please copy file to $PSScriptRoot`n" +
              "Default Icon in use!"
  & $StatusMsg
  $JPArgs = @{Path      = "C:\Windows\Installer"
              ChildPath = 
    "{F895A69B-7C3F-49AD-83FC-A87B31EFF8F3}\PowerShellExe.ico"}
  $IconFile = Join-Path @JPArgs
}


 $WindowTitle = "CM's Set MicroSoft Office 365 " +
                "Update Channel - Version: $PGMVers"
  
[xml]$xaml = @"
<Window
  xmlns =
    "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x  = "http://schemas.microsoft.com/winfx/2006/xaml"
    Name = "Window" 
              Title                 = "$WindowTitle" 
              Icon                  = "$IconFile"
              WindowStartupLocation = "CenterScreen"
              Width                 = "$Form_Width" 
              Height                = "$Form_Height" 
              FontSize              = "18" 
              Background            = "Blue"
              ShowInTaskbar         = "True">

...

</Window>
"@

$NOArgs = @{TypeName     = 'System.Xml.XmlNodeReader'
            ArgumentList = $xaml}
$reader=(New-Object @NOArgs)

Try  {$Form=[Windows.Markup.XamlReader]::Load( $reader )}
Catch{Write-Host "Unable to load Windows.Markup.XamlReader." +
                 "`nDouble-check syntax and ensure " +
                 ".net is installed."
     }

$Form.Topmost = $False
$Form.Cursor = [System.Windows.Input.Cursors]::Hand

填写表单的对象来代替...

© www.soinside.com 2019 - 2024. All rights reserved.