Process.GetProcesses()获取进程 Icon.ExtractAssociatedIcon获取图标
processList.LargeImageList 可以显示
details 下无法显示
[C#] 纯文本查看 复制代码 public process(List<ProcessVo> processes)
{
InitializeComponent();
// 创建一个 ImageList 控件
iconImgList = new ImageList();
// 设置图像大小
iconImgList.ImageSize = new Size(32, 32);
processList.LargeImageList = iconImgList;
foreach (var process in processes)
{
try
{
if (process.Icon != null && process.Name != null && process.Path != null && process.Path != "")
{
iconImgList.Images.Add(process.Id.ToString(), ResizeIcon(process.Icon, 32, 32);
ListViewItem item = new ListViewItem();
item.ImageKey = process.Id.ToString();
item.SubItems.Add(process.Id.ToString());
item.SubItems.Add(process.Name);
item.SubItems.Add(process.Path);
processList.Items.Add(item);
}
}
catch (Exception ex)
{
}
}
}
// 调整图标大小的辅助方法
private Icon ResizeIcon(Icon icon, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Rectangle destRect = new Rectangle(0, 0, width, height);
graphics.DrawIcon(icon, destRect);
}
return Icon.FromHandle(bitmap.GetHicon());
}
|