68 lines
2.0 KiB
C++
Raw Normal View History

2024-04-02 15:36:52 +08:00
#include "define.h"
#include "BMP.h"
2024-07-14 15:09:37 +08:00
bool extracticon2data(const wchar_t *name, void (*cb)(const char *, size_t))
2024-04-02 15:36:52 +08:00
{
2024-01-08 23:37:00 +08:00
HICON h1, h2;
2024-04-02 15:36:52 +08:00
2024-01-08 23:37:00 +08:00
ExtractIconExW(name, 0, &h1, &h2, 1);
2024-04-02 15:36:52 +08:00
if (h1 == 0)
2024-07-14 15:09:37 +08:00
return false;
2024-01-08 23:37:00 +08:00
HDC hdc = GetDC(NULL);
HDC memDC = CreateCompatibleDC(hdc);
ICONINFO iconInfo;
GetIconInfo(h1, &iconInfo);
int iconWidth = iconInfo.xHotspot * 2;
int iconHeight = iconInfo.yHotspot * 2;
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, iconWidth, iconHeight);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(memDC, hBitmap);
DrawIconEx(memDC, 0, 0, h1, iconWidth, iconHeight, 0, NULL, DI_NORMAL);
SelectObject(memDC, hOldBitmap);
DeleteDC(memDC);
2024-04-02 15:36:52 +08:00
BMP bmpp(iconWidth, iconHeight);
2024-01-08 23:37:00 +08:00
BITMAP bmp;
GetObject(hBitmap, sizeof(BITMAP), &bmp);
2024-04-02 15:36:52 +08:00
bmpp.bmp_info_header.bit_count = bmp.bmBitsPixel;
2024-01-08 23:37:00 +08:00
auto dwSize = bmp.bmWidthBytes * bmp.bmHeight;
bmpp.data.resize(dwSize);
2024-04-02 15:36:52 +08:00
auto dataptr = bmpp.data.data();
2024-01-08 23:37:00 +08:00
GetBitmapBits(hBitmap, dwSize, dataptr);
DeleteObject(hBitmap);
2024-04-02 15:36:52 +08:00
std::vector<LONG> tmp;
2024-01-08 23:37:00 +08:00
tmp.resize(bmp.bmWidthBytes);
2024-04-02 15:36:52 +08:00
if (bmp.bmWidthBytes == bmp.bmHeight * 4)
{
bool allalpha0 = true;
for (int i = 0; i < bmpp.data.size() / 4; i++)
{
if (dataptr[i * 4 + 3] != 0)
{
allalpha0 = false;
break;
2024-01-08 23:37:00 +08:00
}
}
2024-04-02 15:36:52 +08:00
if (allalpha0)
{
for (int i = 0; i < bmpp.data.size() / 4; i++)
{
dataptr[i * 4 + 3] = 0xff;
2024-01-08 23:37:00 +08:00
}
}
}
2024-04-02 15:36:52 +08:00
for (int i = 0; i < bmp.bmHeight / 2; i++)
{
2024-01-08 23:37:00 +08:00
memcpy(tmp.data(), dataptr + i * bmp.bmWidthBytes, bmp.bmWidthBytes);
memcpy(dataptr + i * bmp.bmWidthBytes, dataptr + (bmp.bmHeight - 1 - i) * bmp.bmWidthBytes, bmp.bmWidthBytes);
2024-04-02 15:36:52 +08:00
memcpy(dataptr + (bmp.bmHeight - 1 - i) * bmp.bmWidthBytes, tmp.data(), bmp.bmWidthBytes);
}
2024-01-08 23:37:00 +08:00
std::string data;
2024-04-02 15:36:52 +08:00
bmpp.write_tomem(data);
2024-07-14 15:09:37 +08:00
cb(data.c_str(),data.size());
return true;
2024-01-08 23:37:00 +08:00
}