忽略窗口最小化和最大化时发送的WM_SIZE消息

This commit is contained in:
Nomango 2018-07-04 00:27:14 +08:00
parent 950f502b19
commit 8c246df955
3 changed files with 53 additions and 25 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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, /* 窗口宽度 */