+
+HICON CShellLink::CreateShortcutIcon(LPCWSTR wszIconPath, INT IconIndex)
+{
+ const INT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
+ const COLORREF crMask = GetSysColor(COLOR_3DFACE);
+ HDC hDC;
+ HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, 1, 1);
+ HICON hIcon = NULL, hNewIcon = NULL;
+ HICON hShortcut = (HICON)LoadImageW(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT),
+ IMAGE_ICON, cx, cy, 0);
+
+ ::ExtractIconExW(wszIconPath, IconIndex, &hIcon, NULL, 1);
+ if (!hIcon || !hShortcut || !himl)
+ goto cleanup;
+
+ hDC = CreateCompatibleDC(NULL);
+ if (hDC)
+ {
+ // create 32bpp bitmap
+ BITMAPINFO bi;
+ ZeroMemory(&bi, sizeof(bi));
+ bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+ bi.bmiHeader.biWidth = cx;
+ bi.bmiHeader.biHeight = cy;
+ bi.bmiHeader.biPlanes = 1;
+ bi.bmiHeader.biBitCount = 32;
+ LPVOID pvBits;
+ HBITMAP hbm = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
+ if (hbm)
+ {
+ // draw the icon image
+ HGDIOBJ hbmOld = SelectObject(hDC, hbm);
+ {
+ HBRUSH hbr = CreateSolidBrush(crMask);
+ RECT rc = { 0, 0, cx, cy };
+ FillRect(hDC, &rc, hbr);
+ DeleteObject(hbr);
+
+ DrawIconEx(hDC, 0, 0, hIcon, cx, cy, 0, NULL, DI_NORMAL);
+ DrawIconEx(hDC, 0, 0, hShortcut, cx, cy, 0, NULL, DI_NORMAL);
+ }
+ SelectObject(hDC, hbmOld);
+
+ INT iAdded = ImageList_AddMasked(himl, hbm, crMask);
+ hNewIcon = ImageList_GetIcon(himl, iAdded, ILD_NORMAL | ILD_TRANSPARENT);
+
+ DeleteObject(hbm);
+ }
+ DeleteDC(hDC);
+ }
+
+cleanup:
+ if (hIcon)
+ DestroyIcon(hIcon);
+ if (hShortcut)
+ DestroyIcon(hShortcut);
+ if (himl)
+ ImageList_Destroy(himl);
+
+ return hNewIcon;
+}