فهرست منبع

handle ensure window is visible on differnt dpi monitors better

Scott Brogden 7 سال پیش
والد
کامیت
31dc194915
12فایلهای تغییر یافته به همراه127 افزوده شده و 127 حذف شده
  1. 1 2
      CP_Main.cpp
  2. 1 1
      DPI.h
  3. 1 2
      DatabaseUtilities.cpp
  4. 10 5
      ExternalWindowTracker.cpp
  5. 1 2
      MainFrm.cpp
  6. 91 83
      Misc.cpp
  7. 3 2
      Misc.h
  8. 1 3
      Popup.cpp
  9. 7 7
      QPasteWnd.cpp
  10. 5 10
      QuickPaste.cpp
  11. 6 9
      ToolTipEx.cpp
  12. 0 1
      ToolTipEx.h

+ 1 - 2
CP_Main.cpp

@@ -997,8 +997,7 @@ void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
 	pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
 
 	CPoint pt;
-	CRect rcScreen;
-	GetMonitorRect(0, &rcScreen);
+	CRect rcScreen = DefaultMonitorRect();
 	pt = rcScreen.BottomRight();
 
 	CRect cr = pErrorWnd->GetBoundsRect();

+ 1 - 1
DPI.h

@@ -7,7 +7,7 @@ public:
 	{ 
 	}
 
-	void Update(int dpi) { m_dpi = dpi; }
+	void Update(int dpi) { m_dpi = dpi;  _fInitialized = true; }
 
 	// Get screen DPI.
 	int GetDPI() { _Init(); return m_dpi; }

+ 1 - 2
DatabaseUtilities.cpp

@@ -491,8 +491,7 @@ BOOL BackupDB(CString dbPath, CString prefix, CDittoPopupWindow **popUpMsg)
 {
 	if ((*popUpMsg) == NULL)
 	{
-		CRect r;
-		GetMonitorRect(0, r);
+		CRect r = DefaultMonitorRect();
 		*popUpMsg = new CDittoPopupWindow();
 		(*popUpMsg)->Create(CRect(r.right - 400, r.bottom - 130, r.right - 10, r.bottom - 10), NULL);		
 		::SetWindowPos((*popUpMsg)->m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);

+ 10 - 5
ExternalWindowTracker.cpp

@@ -434,26 +434,31 @@ CPoint ExternalWindowTracker::FocusCaret()
 			}
 		}
 
-		if(pt.x < 0 || pt.y < 0)
+		if(pt.x == -1 || pt.y == -1)
 		{
 			if(m_focusWnd != NULL &&
 				m_activeWnd != NULL &&
 				AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE))
 			{
 				BOOL ret = GetCaretPos(&pt);
-				if(ret  && (pt.x > 0 || pt.y > 0))
+				if(ret && (pt.x != 0 && pt.y != 0))
 				{
 					::ClientToScreen(m_focusWnd, &pt);
-					if (pt.y > 0 && pt.x > 0)
+					if (pt.y != 0 && pt.x != 0)
 					{
 						pt.y += 20;
 					}
 					else
 					{
-						pt.x = 0;
-						pt.y = 0;
+						pt.x = -1;
+						pt.y = -1;
 					}
 				}
+				else
+				{
+					pt.x = -1;
+					pt.y = -1;
+				}
 
 				AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
 			}

+ 1 - 2
MainFrm.cpp

@@ -111,8 +111,7 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
 	m_PowerManager.Start(m_hWnd);
 
     ////Center the main window so message boxes are in the center
-    CRect rcScreen;
-    GetMonitorRect(0, &rcScreen);
+    CRect rcScreen = DefaultMonitorRect();
     CPoint cpCenter = rcScreen.CenterPoint();
     MoveWindow(cpCenter.x, cpCenter.x,  1,  1);
 

+ 91 - 83
Misc.cpp

@@ -629,68 +629,6 @@ int GetScreenHeight(void)
 	}
 }
 
-int GetMonitorFromRect(LPRECT lpMonitorRect)
-{
-	// Build up the param
-	MONITOR_ENUM_PARAM	EnumParam;
-	ZeroMemory(&EnumParam, sizeof(EnumParam));
-	EnumParam.lFlags = MONITOR_SEARCH_METOHD;
-	EnumParam.pVirtualRect = lpMonitorRect;
-	EnumParam.iMonitor = -1;
-	
-	// Enum Displays
-	EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (LPARAM)&EnumParam);
-	
-	// Return the result
-	return EnumParam.iMonitor;
-}
-
-void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
-{
-	// Build up the param
-	MONITOR_ENUM_PARAM	EnumParam;
-	ZeroMemory(&EnumParam, sizeof(EnumParam));
-	EnumParam.iMonitor = iMonitor;
-	EnumParam.pVirtualRect = lpDestRect;
-	
-	// Zero out dest rect
-	lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
-	
-	// Enum Displays
-	EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (LPARAM)&EnumParam);
-	
-	// If not successful, default to the screen dimentions
-	if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
-	{
-		lpDestRect->right = GetScreenWidth();
-		lpDestRect->bottom = GetScreenHeight();
-	}
-
-	//adjust the rect for the taskbar
-	APPBARDATA appBarData;
-	appBarData.cbSize=sizeof(appBarData);
-	if (SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData))
-	{
-		switch(appBarData.uEdge)
-		{
-		case ABE_LEFT:
-			lpDestRect->left += appBarData.rc.right - appBarData.rc.left;
-			break;
-		case ABE_RIGHT:
-			lpDestRect->right -= appBarData.rc.right - appBarData.rc.left;
-			break;
-		case ABE_TOP:
-			lpDestRect->top += appBarData.rc.bottom - appBarData.rc.top;
-			break;
-		case ABE_BOTTOM:
-			lpDestRect->bottom -= appBarData.rc.bottom - appBarData.rc.top;
-			break;
-		}
-		return;
-	}
-
-}
-
 /*------------------------------------------------------------------*\
 ID based Globals
 \*------------------------------------------------------------------*/
@@ -769,14 +707,26 @@ BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
 CRect CenterRect(CRect startingRect)
 {
 	CRect crMonitor;
-	int nMonitor = GetMonitorFromRect(&startingRect);
-	if(nMonitor < 0)
+
+	HMONITOR monitorHandle = MonitorFromPoint(startingRect.TopLeft(), MONITOR_DEFAULTTONEAREST);
+	if (monitorHandle == NULL)
 	{
-		GetMonitorRect(0, crMonitor);
+		monitorHandle = MonitorFromPoint(startingRect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+		}
 	}
 	else
 	{
-		GetMonitorRect(nMonitor, crMonitor);
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+		}
 	}
 
 	return CenterRectFromRect(startingRect, crMonitor);
@@ -796,27 +746,85 @@ CRect CenterRectFromRect(CRect startingRect, CRect outerRect)
 	return centerRect;
 }
 
-BOOL EnsureWindowVisible(CRect *pcrRect)
+CRect DefaultMonitorRect()
 {
-	int nMonitor = GetMonitorFromRect(pcrRect);
-	if(nMonitor < 0)
+	CRect crMonitor;
+	CRect invalidRect(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX);
+	HMONITOR monitorHandle = MonitorFromPoint(invalidRect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
+	MONITORINFO lpmi;
+	lpmi.cbSize = sizeof(MONITORINFO);
+	if (GetMonitorInfo(monitorHandle, &lpmi))
 	{
-		GetMonitorRect(0, pcrRect);
-		pcrRect->right = pcrRect->left + 300;
-		pcrRect->bottom = pcrRect->top + 300;
+		crMonitor.CopyRect(&lpmi.rcWork);
+	}
 
-		return TRUE;
+	return crMonitor;
+}
+
+CRect MonitorRectFromRect(CRect rect)
+{
+	BOOL ret = FALSE;
+
+	CRect crMonitor;
+
+	HMONITOR monitorHandle = MonitorFromPoint(rect.TopLeft(), MONITOR_DEFAULTTONEAREST);
+	if (monitorHandle == NULL)
+	{
+		monitorHandle = MonitorFromPoint(rect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+		}
 	}
+	else
+	{
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+		}
+	}
+
+	return crMonitor;
+}
 
+BOOL EnsureWindowVisible(CRect *pcrRect)
+{
 	BOOL ret = FALSE;
 
 	CRect crMonitor;
-	GetMonitorRect(nMonitor, crMonitor);
+	CDPI dpi;
+
+	HMONITOR monitorHandle = MonitorFromRect(pcrRect, MONITOR_DEFAULTTONEAREST);
+	if (monitorHandle == NULL)
+	{
+		monitorHandle = MonitorFromRect(pcrRect, MONITOR_DEFAULTTOPRIMARY);
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+
+			*pcrRect = CenterRectFromRect(*pcrRect, crMonitor);
+		}
+	}
+	else
+	{
+		MONITORINFO lpmi;
+		lpmi.cbSize = sizeof(MONITORINFO);
+		if (GetMonitorInfo(monitorHandle, &lpmi))
+		{
+			crMonitor.CopyRect(&lpmi.rcWork);
+		}
+	}	
 
 	bool movedLeft = false;
 	//Validate the left
 	long lDiff = pcrRect->left - crMonitor.left;
-	if(lDiff < 0)
+	if (lDiff < 0)
 	{
 		pcrRect->left += abs(lDiff);
 		pcrRect->right += abs(lDiff);
@@ -826,9 +834,9 @@ BOOL EnsureWindowVisible(CRect *pcrRect)
 
 	//Right side
 	lDiff = pcrRect->right - crMonitor.right;
-	if(lDiff > 0)
+	if (lDiff > 0)
 	{
-		if(movedLeft == false)
+		if (movedLeft == false)
 		{
 			pcrRect->left -= abs(lDiff);
 		}
@@ -839,7 +847,7 @@ BOOL EnsureWindowVisible(CRect *pcrRect)
 	bool movedTop = false;
 	//Top
 	lDiff = pcrRect->top - crMonitor.top;
-	if(lDiff < 0)
+	if (lDiff < 0)
 	{
 		pcrRect->top += abs(lDiff);
 		pcrRect->bottom += abs(lDiff);
@@ -849,15 +857,15 @@ BOOL EnsureWindowVisible(CRect *pcrRect)
 
 	//Bottom
 	lDiff = pcrRect->bottom - crMonitor.bottom;
-	if(lDiff > 0)
-	{		
-		if(movedTop == false)
+	if (lDiff > 0)
+	{
+		if (movedTop == false)
 		{
 			pcrRect->top -= abs(lDiff);
 		}
-		pcrRect->bottom -= abs(lDiff);		
+		pcrRect->bottom -= abs(lDiff);
 		ret = TRUE;
-	}
+	}	
 
 	return ret;
 }

+ 3 - 2
Misc.h

@@ -110,8 +110,6 @@ BOOL DecryptString(UCHAR *pData, int nLenIn, UCHAR*& pOutput, int &nLenOutput);
 
 int GetScreenWidth();
 int GetScreenHeight();
-void GetMonitorRect(int iMonitor, LPRECT lpDestRect);
-int GetMonitorFromRect(LPRECT lpMonitorRect);
 
 CLIPFORMAT GetFormatID(LPCTSTR cbName);
 CString GetFormatName(CLIPFORMAT cbType);
@@ -122,6 +120,9 @@ CString GetFileName(CString csFileName);
 
 BOOL EnsureWindowVisible(CRect *pcrRect);
 
+CRect DefaultMonitorRect();
+CRect MonitorRectFromRect(CRect rect);
+
 CRect CenterRect(CRect startingRect);
 CRect CenterRectFromRect(CRect startingRect, CRect outerRect);
 

+ 1 - 3
Popup.cpp

@@ -64,9 +64,7 @@ void CPopup::Init()
 	m_bCenterY = false;
 	m_hWndPosRelativeTo = NULL;
 	
-	RECT rcScreen;
-	
-	GetMonitorRect(-1, &rcScreen);
+	RECT rcScreen = DefaultMonitorRect();
 	
 	m_ScreenMaxX = rcScreen.right;
 	m_ScreenMaxY = rcScreen.bottom;

+ 7 - 7
QPasteWnd.cpp

@@ -6688,13 +6688,13 @@ LRESULT CQPasteWnd::OnDpiChanged(WPARAM wParam, LPARAM lParam)
 	int dpi = HIWORD(wParam);	
 	m_DittoWindow.OnDpiChanged(this, dpi);
 		
-	RECT* const prcNewWindow = (RECT*)lParam;
-	SetWindowPos(NULL,
-		prcNewWindow->left,
-		prcNewWindow->top,
-		prcNewWindow->right - prcNewWindow->left,
-		prcNewWindow->bottom - prcNewWindow->top,
-		SWP_NOZORDER | SWP_NOACTIVATE);
+	//RECT* const prcNewWindow = (RECT*)lParam;
+	CRect r(*(RECT*)lParam);
+	if (g_Opt.m_bEnsureEntireWindowCanBeSeen)
+	{
+		EnsureWindowVisible(&r);
+	}
+	SetWindowPos(NULL, r.left, r.top, r.Width(), r.Height(), SWP_NOZORDER | SWP_NOACTIVATE);
 	
 	m_systemMenu.Reset();
 	m_systemMenu.LoadStdImageDPI(m_DittoWindow.m_dpi.GetDPI(), system_menu_2_24, system_menu_2_30, system_menu_2_36, system_menu_2_42, system_menu_2_48, _T("PNG"), system_menu_54, system_menu_60, system_menu_66, system_menu_72, system_menu_78, system_menu_84);

+ 5 - 10
QuickPaste.cpp

@@ -138,7 +138,7 @@ void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboa
 	}
 
 	CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
-	if(ptCaret.x <= 0 || ptCaret.y <= 0)
+	if(ptCaret.x == -1 || ptCaret.y == -1)
 	{
 		CRect cr;
 		::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
@@ -155,16 +155,11 @@ void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboa
 
 			CRect crPoint(point, CSize(1, 1));
 
-			int nMonitor = GetMonitorFromRect(crPoint);
-			if(nMonitor >= 0)
-			{
-				CRect crMonitor;
-				GetMonitorRect(nMonitor, crMonitor);
+			CRect crMonitor = MonitorRectFromRect(crPoint);
 
-				ptCaret = crMonitor.CenterPoint();
-				ptCaret.x -= csSize.cx/2;
-				ptCaret.y -= csSize.cy/2;
-			}
+			ptCaret = crMonitor.CenterPoint();
+			ptCaret.x -= csSize.cx/2;
+			ptCaret.y -= csSize.cy/2;
 		}
 	}
 	

+ 6 - 9
ToolTipEx.cpp

@@ -139,7 +139,6 @@ BOOL CToolTipEx::Create(CWnd *pParentWnd)
 
 BOOL CToolTipEx::Show(CPoint point)
 {
-	m_reducedWindowSize = false;
     if(m_imageViewer.m_pGdiplusBitmap)
     {
 		m_clipData += _T(" | ") + StrF(_T("%d x %d"), m_imageViewer.m_pGdiplusBitmap->GetWidth(), m_imageViewer.m_pGdiplusBitmap->GetHeight());
@@ -221,25 +220,23 @@ BOOL CToolTipEx::Show(CPoint point)
 
 		
 
-		CRect rcScreen;
+		
 
 		ClientToScreen(rect);
 
 		CRect cr(point, point);
-
-		int nMonitor = GetMonitorFromRect(&cr);
-		GetMonitorRect(nMonitor, &rcScreen);
+		CRect rcScreen = MonitorRectFromRect(cr);
 
 		//ensure that we don't go outside the screen
 		if(point.x < 0)
 		{
 			point.x = 5;
-			m_reducedWindowSize = true;
+			//m_reducedWindowSize = true;
 		}
 		if(point.y < 0)
 		{
 			point.y = 5;
-			m_reducedWindowSize = true;
+			//m_reducedWindowSize = true;
 		}
 
 		rcScreen.DeflateRect(0, 0, 5, 5);
@@ -255,12 +252,12 @@ BOOL CToolTipEx::Show(CPoint point)
 		if (rect.right > rcScreen.right)
 		{
 			rect.right = rcScreen.right;
-			m_reducedWindowSize = true;
+			//m_reducedWindowSize = true;
 		}
 		if (rect.bottom > rcScreen.bottom)
 		{
 			rect.bottom = rcScreen.bottom;
-			m_reducedWindowSize = true;
+			//m_reducedWindowSize = true;
 		}
 	}
 

+ 0 - 1
ToolTipEx.h

@@ -82,7 +82,6 @@ protected:
 	CRichEditCtrlEx m_RichEdit;
 	SimpleBrowser m_browser;
 	CWnd *m_pNotifyWnd;
-	bool m_reducedWindowSize;
 	CGdipButton m_optionsButton;
 	int m_clipId;
 	CString m_searchText;