我想这样做,他们在这个问题上做什么:Create an icon in memory with win32 in python但在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
message并传递HICON
作为LPARAM
设置窗口中的图标:
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
你也可以指定ICON_SMALL
以设置窗口的小图标。如果您只设置了一个大图标,它将被缩小以自动创建小图标。但是,如果仅设置小图标,则该窗口将继续使用默认图标作为其大图标。大图标通常为的尺寸为32x32,而小图标通常为的尺寸为16x16。但是,这并不是保证的,所以不要硬编码这些值。如果需要确定它们,请拨打GetSystemMetrics
函数SM_CXICON
和SM_CYICON
以检索大图标的宽度和高度,或者SM_CXSMICON
和SM_CYSMICON
以检索小图标的宽度和高度。
有关使用GDI在Windows中绘图的相当好的教程可用here。如果这是您第一次执行此操作并且没有GDI经验,我建议您仔细阅读。
可能的重复项:http://stackoverflow.com/q/1014101 – 2013-05-09 23:01:27
您可能正在寻找图标叠加层。 – chris 2013-05-09 23:02:58
你的问题非常含糊。如果您正在讨论Windows 7在任务栏中显示图标通知和进度信息,那么您需要查找“ITaskBarList”,“ITaskBarList2”和“ITaskBarList3”shell界面,您可以在MSDN上找到有关该界面的信息。 – 2013-05-09 23:09:31