From 183081ccd3a7cad142bd3cd32c2579e767ecacb8 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Fri, 25 Aug 2017 14:25:16 +0300 Subject: [PATCH] Changes window position only if BOTH top corners are outside ALL monitors. --- modules/highgui/src/window_w32.cpp | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index b1ac3b64a4..929f39a2ce 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -324,15 +324,34 @@ icvLoadWindowPos( const char* name, CvRect& rect ) RegQueryValueEx(hkey, "Width", NULL, &dwType, (BYTE*)&rect.width, &dwSize); RegQueryValueEx(hkey, "Height", NULL, &dwType, (BYTE*)&rect.height, &dwSize); - if( rect.x != (int)CW_USEDEFAULT && (rect.x < -200 || rect.x > 3000) ) - rect.x = 100; - if( rect.y != (int)CW_USEDEFAULT && (rect.y < -200 || rect.y > 3000) ) - rect.y = 100; + // Snap rect into closest monitor in case it falls outside it. // Adi Shavit + // set WIN32 RECT to be the loaded size + POINT tl_w32 = { rect.x, rect.y }; + POINT tr_w32 = { rect.x + rect.width, rect.y }; - if( rect.width != (int)CW_USEDEFAULT && (rect.width < 0 || rect.width > 3000) ) - rect.width = 100; - if( rect.height != (int)CW_USEDEFAULT && (rect.height < 0 || rect.height > 3000) ) - rect.height = 100; + // find monitor containing top-left and top-right corners, or NULL + HMONITOR hMonitor_l = MonitorFromPoint(tl_w32, MONITOR_DEFAULTTONULL); + HMONITOR hMonitor_r = MonitorFromPoint(tr_w32, MONITOR_DEFAULTTONULL); + + // if neither are contained - the move window to origin of closest. + if (NULL == hMonitor_l && NULL == hMonitor_r) + { + // find monitor nearest to top-left corner + HMONITOR hMonitor_closest = MonitorFromPoint(tl_w32, MONITOR_DEFAULTTONEAREST); + + // get coordinates of nearest monitor + MONITORINFO mi; + mi.cbSize = sizeof(mi); + GetMonitorInfo(hMonitor_closest, &mi); + + rect.x = mi.rcWork.left; + rect.y = mi.rcWork.top; + } + + if (rect.width != (int)CW_USEDEFAULT && (rect.width < 0 || rect.width > 3000)) + rect.width = 100; + if (rect.height != (int)CW_USEDEFAULT && (rect.height < 0 || rect.height > 3000)) + rect.height = 100; RegCloseKey(hkey); }