Skip to content

Commit

Permalink
feat: 优化获取程序路径
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Apr 10, 2024
1 parent cc227fd commit f75352d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
6 changes: 2 additions & 4 deletions src/Magpie.App/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ AboutViewModel::AboutViewModel() {

// 异步加载 Logo
([](AboutViewModel* that)->fire_and_forget {
wchar_t exePath[MAX_PATH];
GetModuleFileName(NULL, exePath, MAX_PATH);

auto weakThis = that->get_weak();
SoftwareBitmapSource bitmap;
co_await bitmap.SetBitmapAsync(IconHelper::ExtractIconFromExe(exePath, 256, USER_DEFAULT_SCREEN_DPI));
co_await bitmap.SetBitmapAsync(IconHelper::ExtractIconFromExe(
Win32Utils::GetExePath().c_str(), 256, USER_DEFAULT_SCREEN_DPI));

if (!weakThis.get()) {
co_return;
Expand Down
9 changes: 3 additions & 6 deletions src/Magpie.App/AutoStartHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ static bool CreateAutoStartTask(bool runElevated, const wchar_t* arguments) {
}

// Set the path of the executable to Magpie (passed as CustomActionData).
WCHAR executablePath[MAX_PATH];
GetModuleFileName(NULL, executablePath, MAX_PATH);
hr = execAction->put_Path(wil::make_bstr_nothrow(executablePath).get());
const std::wstring& exePath = Win32Utils::GetExePath();
hr = execAction->put_Path(wil::make_bstr_nothrow(exePath.c_str()).get());
if (FAILED(hr)) {
Logger::Get().ComError("设置可执行文件路径失败", hr);
return false;
Expand Down Expand Up @@ -407,9 +406,7 @@ static bool CreateAutoStartShortcut(const wchar_t* arguments) {
return false;
}

WCHAR executablePath[MAX_PATH];
GetModuleFileName(NULL, executablePath, MAX_PATH);
shellLink->SetPath(executablePath);
shellLink->SetPath(Win32Utils::GetExePath().c_str());

if (arguments) {
shellLink->SetArguments(arguments);
Expand Down
7 changes: 3 additions & 4 deletions src/Magpie.App/TitlebarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "TitleBarControl.g.cpp"
#endif
#include "IconHelper.h"
#include "Win32Utils.h"

using namespace winrt;
using namespace Windows::UI::Xaml::Media::Imaging;
Expand All @@ -13,13 +14,11 @@ namespace winrt::Magpie::App::implementation {
TitleBarControl::TitleBarControl() {
// 异步加载 Logo
[](TitleBarControl* that)->fire_and_forget {
wchar_t exePath[MAX_PATH];
GetModuleFileName(NULL, exePath, MAX_PATH);

auto weakThis = that->get_weak();

SoftwareBitmapSource bitmap;
co_await bitmap.SetBitmapAsync(IconHelper::ExtractIconFromExe(exePath, 40, USER_DEFAULT_SCREEN_DPI));
co_await bitmap.SetBitmapAsync(IconHelper::ExtractIconFromExe(
Win32Utils::GetExePath().c_str(), 40, USER_DEFAULT_SCREEN_DPI));

if (!weakThis.get()) {
co_return;
Expand Down
17 changes: 11 additions & 6 deletions src/Magpie/NotifyIconService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,24 @@ LRESULT NotifyIconService::_NotifyIconWndProc(HWND hWnd, UINT message, WPARAM wP
winrt::hstring mainWindowText = resourceLoader.GetString(L"NotifyIcon_MainWindow");
winrt::hstring exitText = resourceLoader.GetString(L"NotifyIcon_Exit");

HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 1, mainWindowText.c_str());
AppendMenu(hMenu, MF_STRING, 2, exitText.c_str());
wil::unique_hmenu hMenu(CreatePopupMenu());
AppendMenu(hMenu.get(), MF_STRING, 1, mainWindowText.c_str());
AppendMenu(hMenu.get(), MF_STRING, 2, exitText.c_str());

// hWnd 必须为前台窗口才能正确展示弹出菜单
// 即使 hWnd 是隐藏的
SetForegroundWindow(hWnd);

POINT cursorPos;
GetCursorPos(&cursorPos);
BOOL selectedMenuId = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RETURNCMD, cursorPos.x, cursorPos.y, hWnd, nullptr);

DestroyMenu(hMenu);
BOOL selectedMenuId = TrackPopupMenuEx(
hMenu.get(),
TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
cursorPos.x,
cursorPos.y,
hWnd,
nullptr
);

switch (selectedMenuId) {
case 1:
Expand Down
24 changes: 10 additions & 14 deletions src/Magpie/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@

#include "pch.h"
#include "XamlApp.h"
#include "StrUtils.h"
#include "Win32Utils.h"

// 将当前目录设为程序所在目录
static void SetCurDir() noexcept {
wchar_t curDir[MAX_PATH] = { 0 };
GetModuleFileName(NULL, curDir, MAX_PATH);
static void SetWorkingDir() noexcept {
std::wstring exePath = Win32Utils::GetExePath();

for (int i = (int)StrUtils::StrLen(curDir) - 1; i >= 0; --i) {
if (curDir[i] == L'\\' || curDir[i] == L'/') {
break;
} else {
curDir[i] = L'\0';
}
}
FAIL_FAST_IF_FAILED(PathCchRemoveFileSpec(
exePath.data(),
exePath.size() + 1
));

SetCurrentDirectory(curDir);
FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(exePath.c_str()));
}

static void IncreaseTimerResolution() noexcept {
Expand Down Expand Up @@ -72,11 +68,11 @@ int APIENTRY wWinMain(
// 见 https://kennykerr.ca/2018/03/24/cppwinrt-hosting-the-windows-runtime/
winrt::init_apartment(winrt::apartment_type::single_threaded);

SetCurDir();
SetWorkingDir();

auto& app = Magpie::XamlApp::Get();
if (!app.Initialize(hInstance, lpCmdLine)) {
return -1;
return 0;
}

return app.Run();
Expand Down
24 changes: 24 additions & 0 deletions src/Shared/Win32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,27 @@ bool Win32Utils::OpenFolderAndSelectFile(const wchar_t* fileName) noexcept {

return true;
}

const std::wstring& Win32Utils::GetExePath() noexcept {
// 会在日志初始化前调用
static std::wstring result = []() -> std::wstring {
std::wstring exePath;
FAIL_FAST_IF_FAILED(wil::GetModuleFileNameW(NULL, exePath));

if (!wil::is_extended_length_path(exePath.c_str())) {
return exePath;
}

// 去除 \\?\ 前缀
wil::unique_hlocal_string canonicalPath;
FAIL_FAST_IF_FAILED(PathAllocCanonicalize(
exePath.c_str(),
PATHCCH_ALLOW_LONG_PATHS,
canonicalPath.put()
));

return canonicalPath.get();
}();

return result;
}
2 changes: 2 additions & 0 deletions src/Shared/Win32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ struct Win32Utils {
static bool ShellOpen(const wchar_t* path, const wchar_t* parameters = nullptr, bool nonElevated = true) noexcept;
// 不应在主线程调用
static bool OpenFolderAndSelectFile(const wchar_t* fileName) noexcept;

static const std::wstring& GetExePath() noexcept;
};

constexpr bool operator==(const SIZE& l, const SIZE& r) noexcept {
Expand Down

0 comments on commit f75352d

Please sign in to comment.