From cf3dfc4cf41fdba014d84f7a9c5380cc91fa0f43 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 5 Sep 2018 17:24:44 +0800 Subject: [PATCH] Window::CheckMutex rename to CheckUnique --- core/Base/Window.cpp | 28 ++++------------------------ core/e2dmodule.h | 9 ++++----- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index 17b052b0..eb3fd256 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -53,38 +53,18 @@ e2d::Window::~Window() ::CoUninitialize(); } -bool e2d::Window::CheckMutex(const String & mutex) +bool e2d::Window::CheckUnique() { - if (mutex.IsEmpty()) - return false; + HANDLE mutex = ::CreateMutex(nullptr, TRUE, LPCWSTR(L"Easy2DApp-" + title_)); - HANDLE hMutex = ::CreateMutex(nullptr, TRUE, LPCWSTR(L"Easy2DApp-" + mutex)); - - if (hMutex == nullptr) + if (mutex == nullptr) { WARN("CreateMutex Failed!"); return false; } else if (::GetLastError() == ERROR_ALREADY_EXISTS) { - // 关闭进程互斥体 - ::CloseHandle(hMutex); - // 打开游戏窗口 - if (!this->title_.IsEmpty()) - { - // 获取窗口句柄 - HWND hProgramWnd = ::FindWindow(REGISTER_CLASS, (LPCTSTR)title_); - if (hProgramWnd) - { - // 获取窗口显示状态 - WINDOWPLACEMENT wpm; - ::GetWindowPlacement(hProgramWnd, &wpm); - // 将运行的程序窗口还原成正常状态 - wpm.showCmd = SW_SHOW; - ::SetWindowPlacement(hProgramWnd, &wpm); - ::SetWindowPos(hProgramWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); - } - } + ::CloseHandle(mutex); return false; } return true; diff --git a/core/e2dmodule.h b/core/e2dmodule.h index bbd3d2bb..5400a9fd 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -37,11 +37,6 @@ namespace e2d // 销毁窗体实例 static void DestroyInstance(); - // 创建窗体互斥体 - bool CheckMutex( - const String& mutex = L"" /* 进程互斥体名称 */ - ); - // 获取窗体标题 const String& GetTitle() const; @@ -91,6 +86,10 @@ namespace e2d bool enabled ); + // 检测窗口是否唯一 + // 返回值:返回 false 说明游戏已经正在进行,可以防止用户同时打开多个游戏窗口 + bool CheckUnique(); + // 弹出窗口 // 返回值:当窗口包含取消按钮时,返回值表示用户是否点击确认按钮 bool Popup(