diff --git a/src/Magpie.App/AboutViewModel.cpp b/src/Magpie.App/AboutViewModel.cpp index 82da10e6..eb0ce55c 100644 --- a/src/Magpie.App/AboutViewModel.cpp +++ b/src/Magpie.App/AboutViewModel.cpp @@ -42,8 +42,7 @@ AboutViewModel::AboutViewModel() { ([](AboutViewModel* that)->fire_and_forget { auto weakThis = that->get_weak(); SoftwareBitmapSource bitmap; - co_await bitmap.SetBitmapAsync( - IconHelper::ExtractIconFromExe(Win32Utils::GetExePath().c_str(), 256)); + co_await bitmap.SetBitmapAsync(IconHelper::ExtractAppIcon(256)); if (!weakThis.get()) { co_return; diff --git a/src/Magpie.App/IconHelper.cpp b/src/Magpie.App/IconHelper.cpp index 6fb7e1db..72c9be87 100644 --- a/src/Magpie.App/IconHelper.cpp +++ b/src/Magpie.App/IconHelper.cpp @@ -259,6 +259,7 @@ SoftwareBitmap IconHelper::ExtractIconFromExe(const wchar_t* fileName, uint32_t } SoftwareBitmap IconHelper::ExtractAppSmallIcon() { + // 小图标在多处使用,应该缓存 static SoftwareBitmap result{ nullptr }; if (!result) { @@ -284,4 +285,22 @@ SoftwareBitmap IconHelper::ExtractAppSmallIcon() { return result; } +SoftwareBitmap IconHelper::ExtractAppIcon(uint32_t preferredSize) { + /// LoadImage 比 SHDefExtractIcon 快两倍左右 + wil::unique_hicon hIcon((HICON)LoadImage( + GetModuleHandle(nullptr), + MAKEINTRESOURCE(CommonSharedConstants::IDI_APP), + IMAGE_ICON, + preferredSize, + preferredSize, + LR_DEFAULTCOLOR + )); + if (!hIcon) { + Logger::Get().Win32Error("提取程序图标失败"); + return nullptr; + } + + return HIcon2SoftwareBitmap(hIcon.get()); +} + } diff --git a/src/Magpie.App/IconHelper.h b/src/Magpie.App/IconHelper.h index e2e3a677..c600fb95 100644 --- a/src/Magpie.App/IconHelper.h +++ b/src/Magpie.App/IconHelper.h @@ -6,6 +6,7 @@ struct IconHelper { static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize); static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize); static Windows::Graphics::Imaging::SoftwareBitmap ExtractAppSmallIcon(); + static Windows::Graphics::Imaging::SoftwareBitmap ExtractAppIcon(uint32_t preferredSize); }; }