以编程方式更改任务栏图标(Win32, c++)
Changing taskbar icon programmatically (Win32,C++)
我有一个c++ win32程序,我想在运行时编辑任务栏图标以显示有关程序的警报等,但是我对win32 api不太有经验,而且我在网上找不到任何东西。我找到的最接近的是http://www.windows-tech.info/17/52a5bfc45dac0ade.php,它告诉我们如何在运行时将图标从光盘上加载并更改它。
我想做他们在这个问题上所做的:在python中使用win32在内存中创建一个图标,但在c++中没有外部库。
我想做他们在这个问题上所做的:在python中使用win32在内存中创建一个图标,但在c++中没有外部库
由于接受的答案使用wxWidgets库,该库只是Win32 API的包装,因此该解决方案翻译得非常好。
你所需要做的就是使用CreateCompatibleBitmap
函数在内存中创建一个位图。然后,您可以使用标准GDI绘图功能在该位图中绘图。最后,使用CreateIconIndirect
函数创建图标。
最困难的部分是跟踪你的资源,并确保你在完成时释放它们,以防止内存泄漏。如果它全部封装在一个库中,使用RAII来确保对象被正确释放,那就更好了,但是如果你用c++编写C代码,它看起来像这样:
HICON CreateSolidColorIcon(COLORREF iconColor, int width, int height)
{
// Obtain a handle to the screen device context.
HDC hdcScreen = GetDC(NULL);
// Create a memory device context, which we will draw into.
HDC hdcMem = CreateCompatibleDC(hdcScreen);
// Create the bitmap, and select it into the device context for drawing.
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, width, height);
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmp);
// Draw your icon.
//
// For this simple example, we're just drawing a solid color rectangle
// in the specified color with the specified dimensions.
HPEN hpen = CreatePen(PS_SOLID, 1, iconColor);
HPEN hpenOld = (HPEN)SelectObject(hdcMem, hpen);
HBRUSH hbrush = CreateSolidBrush(iconColor);
HBRUSH hbrushOld = (HBRUSH)SelectObject(hdcMem, hbrush);
Rectangle(hdcMem, 0, 0, width, height);
SelectObject(hdcMem, hbrushOld);
SelectObject(hdcMem, hpenOld);
DeleteObject(hbrush);
DeleteObject(hpen);
// Create an icon from the bitmap.
//
// Icons require masks to indicate transparent and opaque areas. Since this
// simple example has no transparent areas, we use a fully opaque mask.
HBITMAP hbmpMask = CreateCompatibleBitmap(hdcScreen, width, height);
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmMask = hbmpMask;
ii.hbmColor = hbmp;
HICON hIcon = CreateIconIndirect(&ii);
DeleteObject(hbmpMask);
// Clean-up.
SelectObject(hdcMem, hbmpOld);
DeleteObject(hbmp);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
// Return the icon.
return hIcon;
}
添加错误检查和代码以在位图上绘制有趣的东西留给读者作为练习。
正如我在上面的评论中所说,一旦你创建了图标,你可以通过发送WM_SETICON
消息并传递HICON
作为LPARAM
来设置窗口的图标:
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
你也可以指定ICON_SMALL
来设置窗口的小图标。如果您只设置了一个大图标,它将被缩小,以自动创建小图标。但是,如果您只设置了小图标,窗口将继续使用默认图标作为其大图标。大图标通常的尺寸为32x32,而小图标通常的尺寸为16x16。然而,这是不能保证的,所以不要硬编码这些值。如果需要确定,可以使用SM_CXICON
和SM_CYICON
调用GetSystemMetrics
函数来检索大图标的宽度和高度,或者调用SM_CXSMICON
和SM_CYSMICON
来检索小图标的宽度和高度。
一个相当好的教程在Windows中使用GDI绘图在这里可用。如果这是你第一次这样做,并且没有GDI的经验,我建议你仔细阅读。
- Win32编译器选项和内存分配
- C++win32 API创建多个类似视口的窗口
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- 检测win32服务创建和删除的最佳方法
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- 将 win32 hbitmap 转换为 winrt softwarebitmap
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- Alt+Enter 在 Win32 应用中,管理大小调整和分辨率
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 从预处理器获取 Windows 版本(C++ Win32)
- Issues with Win32 ReadProcessMemory API
- Win32 发送输入鼠标移动滞后并冻结
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 将 Win32/WinAPI 应用程序移植到 wxWidgets
- Win32 API 控制台光标在 WriteConsole 后不移动
- C++ Win32 Threads
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库