From 00c30681bc57ce18a87bf5d1da6724e9e71baf28 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Wed, 16 May 2012 16:13:24 +0000 Subject: [PATCH] Added Snap-to-Screen Edge feature in the case of multiple monitors (patch by Adi Shavit) --- modules/highgui/src/window_w32.cpp | 39 +++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index c9a40d2..4334c44 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -54,6 +54,9 @@ #include #include +#define COMPILE_MULTIMON_STUBS // Required for multi-monitor support +#include + #ifdef HAVE_OPENGL #include #include @@ -1065,7 +1068,7 @@ CV_IMPL int cvNamedWindow( const char* name, int flags ) icvSetWindowLongPtr( hWnd, CV_USERDATA, window ); icvSetWindowLongPtr( mainhWnd, CV_USERDATA, window ); - // Recalculate window position + // Recalculate window pos icvUpdateWindowPos( window ); result = 1; @@ -1633,7 +1636,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { WINDOWPOS* pos = (WINDOWPOS*)lParam; - // Update the toolbar position/size + // Update the toolbar pos/size if(window->toolbar.toolbar) { RECT rect; @@ -1647,6 +1650,36 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) break; } + case WM_WINDOWPOSCHANGING: + { + // Snap window to screen edges with multi-monitor support. // Adi Shavit + LPWINDOWPOS pos = (LPWINDOWPOS)lParam; + + RECT rect; + GetWindowRect(window->frame, &rect); + + HMONITOR hMonitor; + hMonitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST); + + MONITORINFO mi; + mi.cbSize = sizeof(mi); + GetMonitorInfo(hMonitor, &mi); + + const int SNAP_DISTANCE = 15; + + if (abs(pos->x - mi.rcMonitor.left) <= SNAP_DISTANCE) + pos->x = mi.rcMonitor.left; // snap to left edge + else + if (abs(pos->x + pos->cx - mi.rcMonitor.right) <= SNAP_DISTANCE) + pos->x = mi.rcMonitor.right - pos->cx; // snap to right edge + + if (abs(pos->y - mi.rcMonitor.top) <= SNAP_DISTANCE) + pos->y = mi.rcMonitor.top; // snap to top edge + else + if (abs(pos->y + pos->cy - mi.rcMonitor.bottom) <= SNAP_DISTANCE) + pos->y = mi.rcMonitor.bottom - pos->cy; // snap to bottom edge + } + case WM_ACTIVATE: if(LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) SetFocus(window->hwnd); @@ -2199,7 +2232,7 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, SendMessage(window->toolbar.toolbar, TB_SETBUTTONINFO, (WPARAM)tbs.idCommand, (LPARAM)&tbis); - /* Get button position */ + /* Get button pos */ SendMessage(window->toolbar.toolbar, TB_GETITEMRECT, (WPARAM)tbs.idCommand, (LPARAM)&rect); -- 2.7.4