How to scale Windows context menu icons

204 views Asked by At

I've written a Windows context-menu shell extension in C++ with a bitmap icon attached to the menu item. It's all working fine, except that on some machines (I think ones where the display has a custom scaling factor set) the icon displays at the wrong size:

enter image description here

I've done scaling code which forces the icon to its proper size on all the Windows 10 machines we've tested on, but we're still seeing problems with this on Windows 11 machines with custom scaling factors set:

    HDC screen = GetDC(NULL);
int hSize = GetDeviceCaps(screen, HORZSIZE);
int hRes = GetDeviceCaps(screen, HORZRES);
int scale = ((float(hRes) * 25.4 / hSize) + 64) / 128;
logger << _T("hSize: ") << hSize << _T(" hRes: ") << hRes << _T(" Scale:") << scale << ENDL;

HANDLE imageHandle = LoadImage(g_hInst, MAKEINTRESOURCE(resourceID),
    IMAGE_BITMAP, 16 * scale, 16 * scale, LR_DEFAULTSIZE | LR_LOADTRANSPARENT);
if (!imageHandle)
    logger << _T("Unable to load icon; error ") << GetLastError() << ENDL;

What's the proper way of handling context-menu icon scaling? Is GetDpiForWindow() the preferred solution now?

0

There are 0 answers