Why is my icon being drawn at the wrong size when I call DrawIcon?

Raymond Chen

Some time ago I had a problem with icon drawing. When I tried to draw an icon with Draw­Icon it ended up being drawn at the wrong size. A call to Get­Icon­Info confirmed that the icon was 48×48, but it drew at 32×32.

The answer is documented in a backwards sort of way in the Draw­Icon­Ex function, which says at the bottom,

To duplicate DrawIcon (hDC, X, Y, hIcon), call DrawIconEx as follows:

DrawIconEx (hDC, X, Y, hIcon, 0, 0, 0, NULL,
            DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE);

Aha, if you use Draw­Icon, then the icon size is ignored and it is drawn with DI_DEFAULT­SIZE.

The fix, therefore, was to switch to the Draw­Icon­Ex function so I could remove the DI_DEFAULT­SIZE flag, thereby permitting the icon to be drawn at its actual size.

- DrawIcon(hdc, pt.x, pt.y, hico);
+ DrawIconEx(hdc, pt.x, pt.y, hico, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT);

A bonus quirk of the DI_DEFAULT­SIZE flag (and therefore of the Draw­Icon function) is that the drawing is done at the default icon size, even if you asked it to draw a cursor.

0 comments

Discussion is closed.

Feedback usabilla icon