|
#include <windows.h>
#include <stdio.h>
HICON ExtractIconFromEXE(const char* exePath, int index) {
// 使用ExtractIcon函数提取图标
HICON largeIcon = ExtractIcon(GetModuleHandle(NULL), exePath, index);
return largeIcon;
}
void SaveIconToFile(HICON icon, const char* filePath) {
// 将图标保存为文件
ICONINFO iconInfo;
GetIconInfo(icon, &iconInfo);
BITMAP bmp;
GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bmp);
BITMAPINFOHEADER bih = {0};
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = bmp.bmWidth;
bih.biHeight = -bmp.bmHeight; // 负数表示从上到下的图像
bih.biPlanes = 1;
bih.biBitCount = 32;
bih.biCompression = BI_RGB;
HDC hDC = GetDC(NULL);
HDC hMemDc = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateDIBSection(hMemDc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, NULL, NULL, 0);
HGDIOBJ hOldSel = SelectObject(hMemDc, iconInfo.hbmColor);
BitBlt(hMemDc, 0, 0, bmp.bmWidth, bmp.bmHeight, NULL, 0, 0, WHITENESS);
SelectObject(hMemDc, hOldSel);
// 保存为BMP文件
FILE* fp = fopen(filePath, "wb");
if (fp) {
BITMAPFILEHEADER bfh = {0};
bfh.bfType = 0x4D42; // "BM"
bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmp.bmWidth * bmp.bmHeight * 4;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fwrite(&bfh, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite(&bih, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(iconInfo.hbmColor, bmp.bmWidth * bmp.bmHeight * 4, 1, fp);
fclose(fp);
}
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
DeleteObject(hBitmap);
DeleteDC(hMemDc);
ReleaseDC(NULL, hDC);
}
int main() {
const char* exePath = "C:\\path\\to\\your\\application.exe";
HICON icon = ExtractIconFromEXE(exePath, 0); // 提取第一个图标
if (icon) {
SaveIconToFile(icon, "output.bmp"); // 保存图标到文件
DestroyIcon(icon); // 清理资源
}
return 0;
}
//不知,用炫套用能不能成功?
|
|