@@ -324,15 +324,34 @@ icvLoadWindowPos( const char* name, CvRect& rect )
324
324
RegQueryValueEx (hkey, " Width" , NULL , &dwType, (BYTE*)&rect.width , &dwSize);
325
325
RegQueryValueEx (hkey, " Height" , NULL , &dwType, (BYTE*)&rect.height , &dwSize);
326
326
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 ;
336
355
337
356
RegCloseKey (hkey);
338
357
}
0 commit comments