From 8c246df9559a5bd0a5a66e32019df19158a1cff0 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 4 Jul 2018 00:27:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=BD=E7=95=A5=E7=AA=97=E5=8F=A3=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E5=8C=96=E5=92=8C=E6=9C=80=E5=A4=A7=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E7=9A=84WM=5FSIZE=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Game.cpp | 22 +-------------------- core/Base/Window.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++- core/e2dbase.h | 9 ++++++--- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index e4ce9c92..57f3e102 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -33,7 +33,7 @@ void e2d::Game::destroyInstance() } } -bool e2d::Game::init(const String& mutexName) +bool e2d::Game::init() { if (_initialized) { @@ -41,26 +41,6 @@ bool e2d::Game::init(const String& mutexName) return false; } - if (!mutexName.isEmpty()) - { - // 创建进程互斥体 - String fullMutexName = L"Easy2DApp-" + mutexName; - HANDLE hMutex = ::CreateMutex(nullptr, TRUE, (LPCWSTR)fullMutexName); - - if (hMutex == nullptr) - { - WARN("CreateMutex Failed!"); - } - else if (::GetLastError() == ERROR_ALREADY_EXISTS) - { - // 如果程序已经存在并且正在运行,弹窗提示 - Window::getInstance()->info(L"游戏已在其他窗口中打开!", L"提示"); - // 关闭进程互斥体 - ::CloseHandle(hMutex); - return false; - } - } - // 初始化 COM 组件 CoInitialize(nullptr); diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index d7db0303..e46e1b0f 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -45,6 +45,45 @@ void e2d::Window::destroyInstance() } } +bool e2d::Window::createMutex(const String & mutex) +{ + if (mutex.isEmpty()) + return false; + + // 创建进程互斥体 + String fullMutexName = L"Easy2DApp-" + mutex; + HANDLE hMutex = ::CreateMutex(nullptr, TRUE, (LPCWSTR)fullMutexName); + + if (hMutex == nullptr) + { + WARN("CreateMutex Failed!"); + return false; + } + else if (::GetLastError() == ERROR_ALREADY_EXISTS) + { + // 关闭进程互斥体 + ::CloseHandle(hMutex); + // 打开游戏窗口 + if (!this->_title.isEmpty()) + { + // 获取窗口句柄 + HWND hProgramWnd = ::FindWindow(L"Easy2DApp", (LPCTSTR)this->_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); + } + } + return false; + } + return true; +} + HWND e2d::Window::__create() { // 注册窗口类 @@ -350,14 +389,20 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar { UINT width = LOWORD(lParam); UINT height = HIWORD(lParam); - Window::getInstance()->_size = Size(width, height); + + if (wParam == SIZE_RESTORED) + { + Window::getInstance()->_size = Size(width, height); + } // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 目标适当。它可能会调用失败,但是这里可以忽略有可能的 // 错误,因为这个错误将在下一次调用 EndDraw 时产生 auto pRT = Renderer::getInstance()->getRenderTarget(); if (pRT) + { pRT->Resize(D2D1::SizeU(width, height)); + } } break; diff --git a/core/e2dbase.h b/core/e2dbase.h index e362a104..c4843878 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -21,9 +21,7 @@ public: static void destroyInstance(); // 初始化游戏 - bool init( - const String& mutexName = L"" /* 进程互斥体名称 */ - ); + bool init(); // 启动游戏 void start( @@ -86,6 +84,11 @@ public: // 销毁窗口实例 static void destroyInstance(); + // 创建窗口互斥体 + bool createMutex( + const String& mutex = L"" /* 进程互斥体名称 */ + ); + // 修改窗口大小 void setSize( int width, /* 窗口宽度 */