From e88a049e792386f00d89fd13ad89c7128ec7fcd4 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 15 Oct 2013 13:10:14 +0200 Subject: [PATCH] Windows: Center window on first start --- Core/Config.cpp | 4 +- Windows/WndMainWindow.cpp | 91 ++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 8fc2a5f2f1..e0763debf4 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -82,8 +82,8 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) general->Get("AutoSaveSymbolMap", &bAutoSaveSymbolMap, false); #ifdef _WIN32 general->Get("TopMost", &bTopMost); - general->Get("WindowX", &iWindowX, 40); - general->Get("WindowY", &iWindowY, 100); + general->Get("WindowX", &iWindowX, -1); // -1 tells us to center the window. + general->Get("WindowY", &iWindowY, -1); general->Get("WindowWidth", &iWindowWidth, 0); // 0 will be automatically reset later (need to do the AdjustWindowRect dance). general->Get("WindowHeight", &iWindowHeight, 0); general->Get("PauseOnLostFocus", &bPauseOnLostFocus, false); diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index 6e4ae2d1b8..70fa4f6204 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -317,6 +317,58 @@ namespace MainWindow UpdateScreenScale(); } + RECT DetermineWindowRectangle() { + RECT rc; + + const int screenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN); + const int screenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN); + const int screenX = GetSystemMetrics(SM_XVIRTUALSCREEN); + const int screenY = GetSystemMetrics(SM_YVIRTUALSCREEN); + + if (!g_Config.bFullScreen) { + bool visibleHorizontally = ((g_Config.iWindowX + g_Config.iWindowWidth) >= screenX) && + ((g_Config.iWindowX + g_Config.iWindowWidth) < (screenWidth + g_Config.iWindowWidth)); + + bool visibleVertically = ((g_Config.iWindowY + g_Config.iWindowHeight) >= screenY) && + ((g_Config.iWindowY + g_Config.iWindowHeight) < (screenHeight + g_Config.iWindowHeight)); + + if (!visibleHorizontally) + g_Config.iWindowX = -1; + + if (!visibleVertically) + g_Config.iWindowY = -1; + } + + rc.left = g_Config.iWindowX; + rc.top = g_Config.iWindowY; + + // First, get the w/h right. + if (g_Config.iWindowWidth <= 0 || g_Config.iWindowHeight <= 0) { + RECT rcInner = rc, rcOuter; + GetWindowRectAtResolution(2 * 480, 2 * 272, rcInner, rcOuter); + rc.right = rc.left + (rcOuter.right - rcOuter.left); + rc.bottom = rc.top + (rcOuter.bottom - rcOuter.top); + g_Config.iWindowWidth = rc.right - rc.left; + g_Config.iWindowHeight = rc.bottom - rc.top; + } else { + rc.right = rc.left + g_Config.iWindowWidth; + rc.bottom = rc.top + g_Config.iWindowHeight; + } + + // Then center if necessary. + if (g_Config.iWindowX == -1 && g_Config.iWindowY == -1) { + // Center the window. + g_Config.iWindowX = screenX + (screenWidth - g_Config.iWindowWidth) / 2; + g_Config.iWindowY = screenY + (screenHeight - g_Config.iWindowHeight) / 2; + rc.left = g_Config.iWindowX; + rc.top = g_Config.iWindowY; + rc.right = rc.left + g_Config.iWindowWidth; + rc.bottom = rc.top + g_Config.iWindowHeight; + } + + return rc; + } + void SetIngameMenuItemStates(const GlobalUIState state) { UINT menuEnable = state == UISTATE_INGAME ? MF_ENABLED : MF_GRAYED; @@ -699,45 +751,6 @@ namespace MainWindow windowTitle = title; } - RECT DetermineWindowRectangle() { - RECT rc; - - if (!g_Config.bFullScreen) { - const int screenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN); - const int screenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN); - const int screenX = GetSystemMetrics(SM_XVIRTUALSCREEN); - const int screenY = GetSystemMetrics(SM_YVIRTUALSCREEN); - - bool visibleHorizontally = ((g_Config.iWindowX + g_Config.iWindowWidth) >= screenX) && - ((g_Config.iWindowX + g_Config.iWindowWidth) < (screenWidth + g_Config.iWindowWidth)); - - bool visibleVertically = ((g_Config.iWindowY + g_Config.iWindowHeight) >= screenY) && - ((g_Config.iWindowY + g_Config.iWindowHeight) < (screenHeight + g_Config.iWindowHeight)); - - if (!visibleHorizontally) - g_Config.iWindowX = 0; - - if (!visibleVertically) - g_Config.iWindowY = 0; - } - - rc.left = g_Config.iWindowX; - rc.top = g_Config.iWindowY; - if (g_Config.iWindowWidth <= 0 || g_Config.iWindowHeight <= 0) { - RECT rcInner = rc, rcOuter; - GetWindowRectAtResolution(2 * 480, 2 * 272, rcInner, rcOuter); - rc.right = rc.left + (rcOuter.right - rcOuter.left); - rc.bottom = rc.top + (rcOuter.bottom - rcOuter.top); - g_Config.iWindowWidth = rc.right - rc.left; - g_Config.iWindowHeight = rc.bottom - rc.top; - } else { - rc.right = g_Config.iWindowX + g_Config.iWindowWidth; - rc.bottom = g_Config.iWindowY + g_Config.iWindowHeight; - } - - return rc; - } - BOOL Show(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable.