|
| 1 | +#include "pch.h" |
| 2 | +#include "App.xaml.h" |
| 3 | +#include "MainWindow.xaml.h" |
| 4 | +#include <winrt/Microsoft.UI.Interop.h> // 添加这行 |
| 5 | +#include <microsoft.ui.xaml.window.h> |
| 6 | + |
| 7 | +using namespace winrt; |
| 8 | +using namespace Microsoft::UI::Xaml; |
| 9 | + |
| 10 | +// To learn more about WinUI, the WinUI project structure, |
| 11 | +// and more about our project templates, see: http://aka.ms/winui-project-info. |
| 12 | + |
| 13 | +namespace winrt::MemoryCleaner::implementation |
| 14 | +{ |
| 15 | + // 全局互斥锁句柄 |
| 16 | + static HANDLE hMutex = NULL; |
| 17 | + static const wchar_t* MutexName = L"MemoryCleaner_SingleInstance_Mutex"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// 激活已存在的应用程序窗口 |
| 21 | + /// </summary> |
| 22 | + void ActivateExistingWindow() |
| 23 | + { |
| 24 | + // 查找已存在的窗口 |
| 25 | + HWND hWnd = FindWindowW(L"MemoryCleanerMainWindow", L"MemoryCleaner"); |
| 26 | + if (hWnd) |
| 27 | + { |
| 28 | + // 如果窗口被最小化,则恢复它 |
| 29 | + if (IsIconic(hWnd)) |
| 30 | + { |
| 31 | + ShowWindow(hWnd, SW_RESTORE); |
| 32 | + } |
| 33 | + // 将窗口置于前台 |
| 34 | + SetForegroundWindow(hWnd); |
| 35 | + // 激活窗口 |
| 36 | + SetActiveWindow(hWnd); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initializes the singleton application object. This is the first line of authored code |
| 42 | + /// executed, and as such is the logical equivalent of main() or WinMain(). |
| 43 | + /// </summary> |
| 44 | + App::App() |
| 45 | + { |
| 46 | + // 创建互斥锁,确保只有一个实例运行 |
| 47 | + hMutex = CreateMutexW(NULL, TRUE, MutexName); |
| 48 | + if (hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) |
| 49 | + { |
| 50 | + // 互斥锁已存在,说明已有实例在运行 |
| 51 | + if (hMutex != NULL) |
| 52 | + { |
| 53 | + CloseHandle(hMutex); |
| 54 | + hMutex = NULL; |
| 55 | + } |
| 56 | + |
| 57 | + // 激活已存在的窗口 |
| 58 | + ActivateExistingWindow(); |
| 59 | + |
| 60 | + // 退出当前实例 |
| 61 | + ExitProcess(0); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Xaml objects should not call InitializeComponent during construction. |
| 66 | + // See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent |
| 67 | + |
| 68 | +#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION |
| 69 | + UnhandledException([](IInspectable const&, UnhandledExceptionEventArgs const& e) |
| 70 | + { |
| 71 | + if (IsDebuggerPresent()) |
| 72 | + { |
| 73 | + auto errorMessage = e.Message(); |
| 74 | + __debugbreak(); |
| 75 | + } |
| 76 | + }); |
| 77 | +#endif |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Invoked when the application is launched. |
| 82 | + /// </summary> |
| 83 | + /// <param name="e">Details about the launch request and process.</param> |
| 84 | + void App::OnLaunched([[maybe_unused]] LaunchActivatedEventArgs const& e) |
| 85 | + { |
| 86 | + window = make<MainWindow>(); |
| 87 | + window.Activate(); |
| 88 | + |
| 89 | + // 设置窗口类名和标题,以便其他实例可以找到它 |
| 90 | + if (window) |
| 91 | + { |
| 92 | + auto windowNative = window.try_as<::IWindowNative>(); // 修改这行 |
| 93 | + if (windowNative) |
| 94 | + { |
| 95 | + HWND hWnd = nullptr; |
| 96 | + windowNative->get_WindowHandle(&hWnd); |
| 97 | + if (hWnd) |
| 98 | + { |
| 99 | + // 设置窗口类名 |
| 100 | + SetClassLongW(hWnd, GCLP_HCURSOR, (LONG)LoadCursorW(NULL, IDC_ARROW)); |
| 101 | + // 设置窗口标题 |
| 102 | + SetWindowTextW(hWnd, L"MemoryCleaner"); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments