¿Cómo dibujo un 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! All the "compatible bitmaps", "compatible DCs", etc. stuff are really confusing me.

How do I draw an icon?

preguntado el 01 de julio de 12 a las 22:07

Are you trying to load a static icon into an HICON or are you trying to draw a dynamic/changing icon in the tray? -

@Zac: Most certainly the latter. :) -

Are you using MFC, WTL, Qt? Or just straight Win32? -

@Zac: Did you look at my answer by any chance? -

AAAHHH.. I didn't realize you answered your own question! ( but you haven't accepted it, thats what threw me off.) I had already upvoted it because it was a good answer, but didn't know if it met the original posters criteria. -

1 Respuestas

Without getting into too much detail, you can use the following C++ class.

Utiliza el Biblioteca de plantillas de Windows, but it should be very simple to convert it into plain C.

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

    CIconDC(int cx = GetSystemMetrics(SM_CXSMICON),  // width
            int cy = GetSystemMetrics(SM_CYSMICON),  // height
            HDC templateDC = CClientDC(NULL))  // automatically calls ReleaseDC
        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 democracia fácil:

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!

Respondido 02 Jul 12, 01:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.