From 79432b5d56bd7a4aa996e82ef67cad687d1df411 Mon Sep 17 00:00:00 2001 From: Xu Date: Sun, 23 Jun 2024 17:32:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B8=B8=E6=88=8F=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BD=A9=E8=89=B2=E6=8E=A9=E7=A0=81=E5=9B=BE?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Magpie.App/IconHelper.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Magpie.App/IconHelper.cpp b/src/Magpie.App/IconHelper.cpp index 03f629b8..9d919f92 100644 --- a/src/Magpie.App/IconHelper.cpp +++ b/src/Magpie.App/IconHelper.cpp @@ -35,8 +35,7 @@ static bool CopyPixelsOfHBmp(HBITMAP hBmp, LONG width, LONG height, void* data) } static SoftwareBitmap HIcon2SoftwareBitmap(HICON hIcon) { - // 单色图标: 不处理 - // 彩色掩码图标: 忽略掩码 + // 支持彩色光标和彩色掩码图标,不支持单色图标 ICONINFO iconInfo{}; if (!GetIconInfo(hIcon, &iconInfo)) { @@ -86,8 +85,26 @@ static SoftwareBitmap HIcon2SoftwareBitmap(HICON hIcon) { } } else { // 彩色掩码图标 - for (uint32_t i = 3; i < pixelsSize; i += 4) { - pixels[i] = 255; + if (iconInfo.hbmMask) { + std::unique_ptr maskData = std::make_unique(pixelsSize); + if (!CopyPixelsOfHBmp(iconInfo.hbmMask, bmp.bmWidth, bmp.bmHeight, maskData.get())) { + return nullptr; + } + + for (uint32_t i = 0; i < pixelsSize; i += 4) { + // hbmMask 表示是否应用掩码 + // 如果需要应用掩码而掩码不为零,那么这个图标无法转换为彩色图标,这种情况下直接忽略掩码 + if (maskData[i] != 0 && pixels[i] == 0 && pixels[i + 1] == 0 && pixels[i + 2] == 0) { + // 掩码全为 0 表示透明像素 + std::memset(pixels + i, 0, 4); + } else { + pixels[i + 3] = 255; + } + } + } else { + for (uint32_t i = 3; i < pixelsSize; i += 4) { + pixels[i] = 255; + } } } }