Changes window position only if BOTH top corners are outside ALL monitors.

This commit is contained in:
Adi Shavit 2017-08-25 14:25:16 +03:00
parent 8ffa29473f
commit 183081ccd3

View File

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