commit 5061dbb4c3b3ebc1f270604a9f22017e701b0d9a
parent 775ad7f81cb0ca8cd2bb40cd0b48919381ee0ce5
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 19 Jan 2016 14:16:09 -0800
fix and enhance window centering logic
Diffstat:
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/src/dialog.cpp b/src/dialog.cpp
@@ -17,6 +17,10 @@
#include "dialog.hpp"
+#include <algorithm>
+
+using namespace std;
+
DialogMap Dialog::s_instances;
WDL_DLGRET Dialog::Proc(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
@@ -132,13 +136,21 @@ void Dialog::center()
GetWindowRect(m_handle, &dialogRect);
GetWindowRect(m_parent, &parentRect);
- int left = parentRect.right / 2;
- left -= (dialogRect.right - dialogRect.left) / 2;
+ const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
+ const int parentWidth = parentRect.right - parentRect.left;
+ const int dialogWidth = dialogRect.right - dialogRect.left;
+
+ int left = (parentWidth - dialogWidth) / 2;
+ left = min(left + (int)parentRect.left, screenWidth - dialogWidth);
+
+ const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
+ const int parentHeight = parentRect.bottom - parentRect.top;
+ const int dialogHeight = dialogRect.bottom - dialogRect.top;
- int top = parentRect.bottom / 2;
- top -= (dialogRect.bottom - dialogRect.top) / 2;
+ int top = (parentHeight - dialogHeight) / 2;
+ top = min(top + (int)parentRect.top, screenHeight - dialogHeight);
- SetWindowPos(m_handle, HWND_TOP, left, top, 0, 0, SWP_NOSIZE);
+ SetWindowPos(m_handle, HWND_TOP, max(0, left), max(0, top), 0, 0, SWP_NOSIZE);
}
void Dialog::setFocus()