Skip to content

Commit

Permalink
perf: 优化关于页面加载 logo 的性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Oct 22, 2024
1 parent 77d519a commit b8206aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Magpie.App/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/Magpie.App/IconHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ SoftwareBitmap IconHelper::ExtractIconFromExe(const wchar_t* fileName, uint32_t
}

SoftwareBitmap IconHelper::ExtractAppSmallIcon() {
// 小图标在多处使用,应该缓存
static SoftwareBitmap result{ nullptr };

if (!result) {
Expand All @@ -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());
}

}
1 change: 1 addition & 0 deletions src/Magpie.App/IconHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

}

0 comments on commit b8206aa

Please sign in to comment.