Skip to content

Commit b00dff8

Browse files
committed
Merge pull request opencv#9456 from adishavit:issue_8840/CorrectlyRestoreWindowPosOnMultipleMonitors
2 parents 4a81492 + 183081c commit b00dff8

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

modules/highgui/src/window_w32.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,34 @@ icvLoadWindowPos( const char* name, CvRect& rect )
324324
RegQueryValueEx(hkey, "Width", NULL, &dwType, (BYTE*)&rect.width, &dwSize);
325325
RegQueryValueEx(hkey, "Height", NULL, &dwType, (BYTE*)&rect.height, &dwSize);
326326

327-
if( rect.x != (int)CW_USEDEFAULT && (rect.x < -200 || rect.x > 3000) )
328-
rect.x = 100;
329-
if( rect.y != (int)CW_USEDEFAULT && (rect.y < -200 || rect.y > 3000) )
330-
rect.y = 100;
331-
332-
if( rect.width != (int)CW_USEDEFAULT && (rect.width < 0 || rect.width > 3000) )
333-
rect.width = 100;
334-
if( rect.height != (int)CW_USEDEFAULT && (rect.height < 0 || rect.height > 3000) )
335-
rect.height = 100;
327+
// Snap rect into closest monitor in case it falls outside it. // Adi Shavit
328+
// set WIN32 RECT to be the loaded size
329+
POINT tl_w32 = { rect.x, rect.y };
330+
POINT tr_w32 = { rect.x + rect.width, rect.y };
331+
332+
// find monitor containing top-left and top-right corners, or NULL
333+
HMONITOR hMonitor_l = MonitorFromPoint(tl_w32, MONITOR_DEFAULTTONULL);
334+
HMONITOR hMonitor_r = MonitorFromPoint(tr_w32, MONITOR_DEFAULTTONULL);
335+
336+
// if neither are contained - the move window to origin of closest.
337+
if (NULL == hMonitor_l && NULL == hMonitor_r)
338+
{
339+
// find monitor nearest to top-left corner
340+
HMONITOR hMonitor_closest = MonitorFromPoint(tl_w32, MONITOR_DEFAULTTONEAREST);
341+
342+
// get coordinates of nearest monitor
343+
MONITORINFO mi;
344+
mi.cbSize = sizeof(mi);
345+
GetMonitorInfo(hMonitor_closest, &mi);
346+
347+
rect.x = mi.rcWork.left;
348+
rect.y = mi.rcWork.top;
349+
}
350+
351+
if (rect.width != (int)CW_USEDEFAULT && (rect.width < 0 || rect.width > 3000))
352+
rect.width = 100;
353+
if (rect.height != (int)CW_USEDEFAULT && (rect.height < 0 || rect.height > 3000))
354+
rect.height = 100;
336355

337356
RegCloseKey(hkey);
338357
}

0 commit comments

Comments
 (0)