C++ 我怎样画一个HICON?

C++ 我怎样画一个HICON?,c++,c,winapi,visual-c++,icons,C++,C,Winapi,Visual C++,Icons,我想在系统托盘中显示一个图标 它看起来应该很简单,但我不知道如何创建一个HICON并绘制它!所有的“兼容位图”、“兼容DC”等东西都让我很困惑

我想在系统托盘中显示一个图标

它看起来应该很简单,但我不知道如何创建一个
HICON
并绘制它!所有的“兼容位图”、“兼容DC”等东西都让我很困惑


<如何绘制图标?

而不涉及太多的细节,可以使用下面的C++类。

它使用,但将其转换为普通C应该非常简单

using namespace WTL;
class CIconDC : public CDC
{
public:
    HBITMAP hBmpOld;

    CIconDC(int cx = GetSystemMetrics(SM_CXSMICON),  // width
            int cy = GetSystemMetrics(SM_CYSMICON),  // height
            HDC templateDC = CClientDC(NULL))  // automatically calls ReleaseDC
    {
        this->CreateCompatibleDC(templateDC);
        hBmpOld = this->SelectBitmap(CreateCompatibleBitmap(templateDC, cx, cy));
    }

    ~CIconDC() { DeleteObject(this->SelectBitmap(hBmpOld)); }

    HICON CreateIcon() const
    {
        // temporarily swap bitmaps to get handle of current bitmap
        HBITMAP hBitmap = this->GetCurrentBitmap();
        ICONINFO ii = { TRUE, 0, 0, hBitmap, hBitmap };
        return CreateIconIndirect(&ii);
    }
};
使用该类非常容易:

CIconDC dc;
dc.LineTo(10, 10);  // for example -- you can do whatever you want with the DC
CIcon hIcon = dc.CreateIcon();  // converted to an HICON!

您是想将静态图标加载到HICON中,还是想在托盘中绘制动态/变化的图标?@Zac:当然是后者。:)您使用的是MFC、WTL、Qt吗?还是直接用Win32?@Zac:你有没有碰巧看到我的答案?啊哈。。我不知道你回答了你自己的问题!(但你没有接受,这让我很反感。)我已经投了更高的票,因为这是一个好的答案,但不知道它是否符合最初的标准。