You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为PowerShell中WScript.Shell的Popup窗口设置标题栏图标?

为WScript.Shell Popup窗口添加标题栏图标可行吗?

结论

WScript.Shell的Popup方法本身不支持自定义标题栏窗口图标。它调用的是系统原生消息框,标题栏图标由系统或方法参数中的消息图标常量(如你代码里的48、64)决定,但这些常量仅控制弹窗内部的提示图标,和标题栏的窗口图标无关。

你代码中使用的0 + 4096 + 48参数里:

  • 48对应警告图标(显示在弹窗内容区域)
  • 64对应信息图标(同样在内容区域)
  • 4096设置弹窗始终置顶
    这些参数都无法影响标题栏的窗口图标。

替代方案:使用Windows Forms创建自定义弹窗

如果需要自定义标题栏图标,可通过PowerShell调用Windows Forms创建自定义弹窗,完全控制窗口样式和图标。

以下是修改后的完整代码,替换原有的Popup调用:

Set-StrictMode -Version latest

# 加载Windows Forms程序集
Add-Type -AssemblyName System.Windows.Forms

function Show-CustomPopup {
    param(
        [string]$Message,
        [string]$Title,
        [int]$TimeoutSeconds,
        [string]$IconPath
    )

    # 创建主窗口
    $form = New-Object System.Windows.Forms.Form
    $form.Text = $Title
    $form.Size = New-Object System.Drawing.Size(400, 150)
    $form.StartPosition = "CenterScreen"
    $form.TopMost = $true

    # 设置标题栏图标(需传入ICO文件路径)
    if ($IconPath -and (Test-Path $IconPath)) {
        $form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($IconPath)
    }

    # 添加消息文本标签
    $label = New-Object System.Windows.Forms.Label
    $label.Text = $Message
    $label.AutoSize = $true
    $label.Location = New-Object System.Drawing.Point(20, 20)
    $form.Controls.Add($label)

    # 设置自动关闭定时器
    $timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = $TimeoutSeconds * 1000
    $timer.Add_Tick({
        $form.Close()
        $timer.Dispose()
    })
    $timer.Start()

    # 显示弹窗
    $form.ShowDialog() | Out-Null
    $form.Dispose()
}

function monitorProcesses{
    DO{   
        foreach($name in $processList){
            if((Get-Process $name -ErrorAction SilentlyContinue) -eq $null){
                # 记录已结束的进程
                if($finishedList -notcontains($name)){
                    $finishedList.Add($name) | Out-Null
                }
            }
        }
    }while($finishedList.Count -ne $processList.Count)
    
    # 调用自定义完成弹窗
    Show-CustomPopup -Message "All startup processes have completed. It is safe to start the program" `
        -Title "All Processes Complete" `
        -TimeoutSeconds 10 `
        -IconPath "C:\your\icon\path\custom.ico"
}


#____________________________________ 主逻辑 ____________________________________#

# 初始化进程列表和已结束进程清单
$processList =  @('WINWORD', 'msedge')
[System.Collections.ArrayList]$finishedList = @() 

# 调用自定义等待弹窗
Show-CustomPopup -Message "Please wait for all startup processes to end before starting the program" `
    -Title "Please Wait" `
    -TimeoutSeconds 10 `
    -IconPath "C:\your\icon\path\custom.ico"

monitorProcesses 

使用说明

  1. 将-IconPath参数的值替换为你的ICO文件实际路径(支持.ico、.exe、.dll等包含图标的文件)
  2. 自定义弹窗支持自动关闭(和原Popup的超时功能一致),且可自由调整窗口大小、文本样式等
  3. 如果不需要自定义图标,可移除-IconPath参数,窗口会使用Windows Forms默认的标题栏图标

内容的提问来源于stack exchange,提问作者AngelOfChaos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:15:35