简体   繁体   English

我如何画一个HICON?

[英]How do I draw an HICON?

I want to display an icon in the system tray. 我想在系统托盘中显示一个图标。

It seems like it should be so simple, but I can't figure out how to create an HICON and draw to it! 它似乎应该如此简单,但我无法弄清楚如何创建一个HICON并绘制它! All the "compatible bitmaps", "compatible DCs", etc. stuff are really confusing me. 所有“兼容的位图”,“兼容的DC”等东西真的让我很困惑。

How do I draw an icon? 如何绘制图标?

Without getting into too much detail, you can use the following C++ class. 在不深入细节的情况下,您可以使用以下C ++类。

It uses the Windows Template Library , but it should be very simple to convert it into plain C. 它使用Windows模板库 ,但将它转换为普通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);
    }
};

Using the class is really easy: 使用该类非常简单:

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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM