以编程方式更改任务栏图标(Win32,C++) [英] Changing taskbar icon programmatically (Win32,C++)

查看:162
本文介绍了以编程方式更改任务栏图标(Win32,C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ win32 程序,我想在运行时编辑任务栏图标以显示有关该程序的警报等,但是我对 win32 api 不太熟悉,而且我一直无法在网上找到任何东西.我找到的最接近的是 http://www.windows-tech.info/17/52a5bfc45dac0ade.php 说明如何在运行时从光盘加载图标并更改它.

I have a C++ win32 program, and I'd like to edit the taskbar icon at runtime to display alerts, etc about the program, however I'm not too experienced with the win32 api, and I haven't been able to find anything online. The closest I've found is http://www.windows-tech.info/17/52a5bfc45dac0ade.php which tells how to load the icon off the disc at runtime and change it.

我想做他们在这个问题中所做的:在python中使用win32在内存中创建一个图标但在C++中并且没有外部库.

I would like to do what they do in this question: Create an icon in memory with win32 in python but in C++ and without an external library.

推荐答案

我想做他们在这个问题中所做的:在python中使用win32在内存中创建一个图标但在C++中并且没有外部库

I would like to do what they do in this question: Create an icon in memory with win32 in python but in C++ and without an external library

由于接受的答案使用 wxWidgets 库,它只是 Win32 API 的包装器,因此该解决方案的翻译非常好.

Since the accepted answer uses the wxWidgets library, which is just a wrapper of the Win32 API, the solution translates pretty nicely.

您需要做的就是使用 CreateCompatibleBitmap 函数.然后,您可以使用标准 GDI 绘图函数绘制该位图.最后,您使用 CreateIconIndirect 函数.

All you need to do is create a bitmap in memory using the CreateCompatibleBitmap function. Then you can draw into that bitmap using the standard GDI drawing functions. Finally, you create the icon using the CreateIconIndirect function.

最困难的部分是跟踪您的资源并确保在完成后释放它们以防止内存泄漏.如果所有内容都包含在一个使用 RAII 的库中,以确保正确释放对象,那就更好了,但是如果您使用 C++ 编写 C 代码,则它看起来像这样:

The hardest part is keeping track of your resources and making sure that you free them all when you're finished to prevent memory leaks. It's way better if it's all wrapped up in a library that makes use of RAII to ensure the objects are properly freed, but if you're writing C code in C++, it would look like this:

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;
}

添加错误检查和在位图上绘制有趣内容的代码留给读者作为练习.

Adding the error checking and the code to draw something interesting onto the bitmap is left as an exercise for the reader.

正如我在上面的评论中所说的,一旦你创建了图标,你就可以通过发送一个 WM_SETICON 消息 并将 HICON 作为 LPARAM 传递:

As I said in a comment above, once you have created the icon, you can set the icon for a window by sending it a WM_SETICON message and passing the HICON as the LPARAM:

SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);

你也可以指定ICON_SMALL来设置窗口的小图标.如果你只设置一个大图标,它会被缩小以自动创建小图标.但是,如果您只设置小图标,窗口将继续使用默认图标作为其大图标.大图标通常的尺寸为 32x32,而小图标通常的尺寸为 16x16.但是,这并不能保证,因此不要对这些值进行硬编码.如果您需要确定它们,请调用 GetSystemMetrics 函数与 SM_CXICONSM_CYICON 一起获取大图标的宽度和高度,或者 SM_CXSMICONSM_CYSMICON 获取小图标的宽度和高度.

You can also specify ICON_SMALL in order to set the window's small icon. If you set only a big icon, it will be scaled down to create the small icon automatically. However, if you set only the small icon, the window will continue to use the default icon as its big icon. Big icons typically have a dimension of 32x32, while small icons typically have a dimension of 16x16. This is not, however, guaranteed, so do not hardcode these values. If you need to determine them, call the GetSystemMetrics function with SM_CXICON and SM_CYICON to retrieve the width and height of big icons, or SM_CXSMICON and SM_CYSMICON to retrieve the width and height of small icons.

关于使用 GDI 在 Windows 中绘图的相当不错的教程可用 这里.如果这是您第一次这样做并且之前没有使用 GDI 的经验,我建议您通读一遍.

A fairly good tutorial on drawing in Windows using GDI is available here. I recommend reading it thoroughly if this is your first time doing this and have no prior experience with GDI.

这篇关于以编程方式更改任务栏图标(Win32,C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆