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

如何用PowerShell Forms实现任务栏程序覆盖图标?解决属性报错

WinForms实现任务栏按钮覆盖图标方案

System.Windows.Forms.Form没有TaskbarItemInfo属性——这个属性是WPF窗口的专属API。要在WinForms中实现任务栏按钮的覆盖图标,需要调用Windows原生Shell API来完成。

可行实现代码

Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class TaskbarHelper {
    [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
    public static extern int SetTaskbarOverlayIcon(IntPtr hwnd, IntPtr hIcon, string pszDescription);
}
"@

[System.Windows.Forms.Application]::EnableVisualStyles()
$logo=[System.Drawing.Image]::Fromfile('C:\programdata\logo.png')
$icon=New-Object system.drawing.icon('C:\programdata\icon.ico')
$form=New-Object system.Windows.Forms.Form
$form.Width=$logo.Width
$form.Height=$logo.Height
$form.TopMost=$true
$form.BackgroundImage=$logo
$form.AllowTransparency=$true
$form.TransparencyKey=$form.BackColor
$form.StartPosition=[System.Windows.Forms.FormStartPosition]::CenterScreen
$form.FormBorderStyle=[System.Windows.Forms.FormBorderStyle]::None
$form.Icon=$icon

# 设置任务栏覆盖图标
[TaskbarHelper]::SetTaskbarOverlayIcon($form.Handle, $icon.Handle, "自定义描述")

# 窗口关闭时清理图标资源
$form.Add_FormClosed({
    $icon.Dispose()
    $logo.Dispose()
})

$form.ShowDialog()

说明

  • 代码通过Add-Type导入SetTaskbarOverlayIcon API,该API用于为指定窗口的任务栏按钮设置覆盖图标
  • 调用API时需传入窗口句柄、图标句柄以及描述文本
  • 窗口关闭时记得释放图标和图片资源,避免内存泄漏
  • 该功能仅支持Windows Vista及以上版本系统

内容的提问来源于stack exchange,提问作者PlayLORD-SysOp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 09:05:21