Начинающий
-
Автор темы
- #1
написал простой парсер иконок, но не могу понять как гетать иконки больше чем 48х48
parser:
HICON WindowManager::GetWindowIcon(HWND hwnd) {
HICON bestIcon = nullptr;
auto EvaluateIcon = [&](HICON icon) {
if (icon) {
ICONINFO iconInfo;
BITMAP bmp;
if (GetIconInfo(icon, &iconInfo) && GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bmp)) {
int iconWidth = bmp.bmWidth;
int iconHeight = bmp.bmHeight;
char debugMessage[256];
sprintf_s(debugMessage, "Icon size: %dx%d\n", iconWidth, iconHeight);
OutputDebugStringA(debugMessage);
if (!bestIcon || (iconWidth * iconHeight > bmp.bmWidth * bmp.bmHeight)) {
if (bestIcon) {
DestroyIcon(bestIcon);
}
bestIcon = icon;
}
else {
DestroyIcon(icon);
}
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
}
else {
OutputDebugStringA("GetIconInfo failed for icon.\n");
}
}
else {
OutputDebugStringA("Icon is null.\n");
}
};
DPI_AWARENESS_CONTEXT oldDpiContext = nullptr;
if (IsWindows10OrGreater()) {
oldDpiContext = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
OutputDebugStringA("attempting to retrieve icons via sendMessage and getclasslongptr...\n");
EvaluateIcon(reinterpret_cast<HICON>(SendMessage(hwnd, WM_GETICON, ICON_BIG, 0)));
EvaluateIcon(reinterpret_cast<HICON>(SendMessage(hwnd, WM_GETICON, ICON_SMALL2, 0)));
EvaluateIcon(reinterpret_cast<HICON>(GetClassLongPtr(hwnd, GCLP_HICON)));
EvaluateIcon(reinterpret_cast<HICON>(GetClassLongPtr(hwnd, GCLP_HICONSM)));
if (!bestIcon) {
TCHAR exePath[MAX_PATH] = { 0 };
if (GetWindowModuleFileName(hwnd, exePath, MAX_PATH)) {
OutputDebugStringA("Retrieving icon from executable file...\n");
SHFILEINFO sfi = {};
if (SHGetFileInfo(exePath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_LARGEICON)) {
EvaluateIcon(sfi.hIcon);
}
if (!bestIcon) {
HICON extractedIcon = nullptr;
if (SUCCEEDED(SHDefExtractIcon(exePath, 0, 0, &extractedIcon, nullptr, MAKELONG(256, 256)))) {
EvaluateIcon(extractedIcon);
}
else {
OutputDebugStringA("Failed to extract icon with SHDefExtractIcon.\n");
}
}
}
else {
OutputDebugStringA("GetWindowModuleFileName failed to retrieve executable path.\n");
}
}
if (oldDpiContext) {
SetThreadDpiAwarenessContext(oldDpiContext);
}
if (!bestIcon) {
OutputDebugStringA("Window has no high-res icon. Loading default icon.\n");
bestIcon = LoadIcon(NULL, IDI_APPLICATION);
}
return bestIcon;
}