Browse Source

zoom in on image with contorl - mouse wheel

sabrogden 6 years ago
parent
commit
4630af3dcd
5 changed files with 77 additions and 26 deletions
  1. 22 21
      ImageViewer.cpp
  2. 1 0
      Misc.h
  3. 21 3
      ScrollHelper.cpp
  4. 30 1
      ToolTipEx.cpp
  5. 3 1
      ToolTipEx.h

+ 22 - 21
ImageViewer.cpp

@@ -85,16 +85,33 @@ void CImageViewer::UpdateBitmapSize(bool setScale)
 				CRect rect;
 				CRect rect;
 				GetClientRect(rect);
 				GetClientRect(rect);
 
 
+				SCROLLINFO si;
+				if (this->GetScrollInfo(SB_HORZ, &si) && si.nPage > 0)
+				{
+					int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
+					rect.right += cxSB;
+					int cySB = ::GetSystemMetrics(SM_CYHSCROLL);
+					rect.bottom += cySB;
+				}
+
 				double w = m_pGdiplusBitmap->GetWidth();
 				double w = m_pGdiplusBitmap->GetWidth();
 				if (w > 0)
 				if (w > 0)
 				{
 				{
 					m_scale = rect.Width() / w;
 					m_scale = rect.Width() / w;
 				}
 				}
 			}
 			}
+			else
+			{
+				m_scale = 1;
+			}
+
+			m_scrollHelper.ScrollToOrigin(true, true);
 		}
 		}
 
 
-		m_scrollHelper.AttachWnd(this);
+		m_scrollHelper.AttachWnd(this);		
 		m_scrollHelper.SetDisplaySize(m_pGdiplusBitmap->GetWidth(), m_pGdiplusBitmap->GetHeight(), m_scale);		
 		m_scrollHelper.SetDisplaySize(m_pGdiplusBitmap->GetWidth(), m_pGdiplusBitmap->GetHeight(), m_scale);		
+
+		this->GetParent()->PostMessage(WM_REFRESH_FOOTER, 0, 0);
 	}
 	}
 }
 }
 
 
@@ -156,7 +173,7 @@ BOOL CImageViewer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
 {
 {
 	OutputDebugString(_T("OnMouseWheel\r\n"));
 	OutputDebugString(_T("OnMouseWheel\r\n"));
 
 
-	if (nFlags == 8)
+	if (nFlags == MK_CONTROL)
 	{
 	{
 		this->LockWindowUpdate();
 		this->LockWindowUpdate();
 		CPoint delta;
 		CPoint delta;
@@ -171,25 +188,11 @@ BOOL CImageViewer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
 			m_scale -= .1;
 			m_scale -= .1;
 		}
 		}
 
 
-		POINT pointInImage;
-		pointInImage.x = pt.x;
-		pointInImage.y = pt.y;
-
-		::ScreenToClient(m_hWnd, &pointInImage);
-
-		//point in image is the scrolled pos (un scaled) and the current mouse point in the image (scalled)
-		//so unscale the point in image and add it to the scrolled pos
-		pointInImage.x = (pointInImage.x * (1 / oldScale)) + m_scrollHelper.GetScrollPos().cx;
-		pointInImage.y = (pointInImage.y * (1 / oldScale)) + m_scrollHelper.GetScrollPos().cy;
-
+		::ScreenToClient(m_hWnd, &pt);
 		UpdateBitmapSize(false);
 		UpdateBitmapSize(false);
 
 
-		//find the difference between the scaled point and and not scaled
-		int xScroll = pointInImage.x - (pointInImage.x * (1 / m_scale));
-		int yScroll = pointInImage.y - (pointInImage.y * (1 / m_scale));
-
-		delta.x = xScroll - m_scrollHelper.GetScrollPos().cx;
-		delta.y = yScroll - m_scrollHelper.GetScrollPos().cy;
+		delta.x = (pt.x * (1 / oldScale)) - (pt.x * (1 / m_scale));
+		delta.y = (pt.y * (1 / oldScale)) - (pt.y * (1 / m_scale));
 
 
 		m_scrollHelper.Update(delta);
 		m_scrollHelper.Update(delta);
 
 
@@ -252,8 +255,6 @@ void CImageViewer::OnLButtonUp(UINT nFlags, CPoint point)
 	if (this->m_pGdiplusBitmap &&
 	if (this->m_pGdiplusBitmap &&
 		m_hoveringOverImage)
 		m_hoveringOverImage)
 	{
 	{
-		m_scale = 1;
-
 		CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
 		CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
 		
 		
 		UpdateBitmapSize(true);
 		UpdateBitmapSize(true);

+ 1 - 0
Misc.h

@@ -160,6 +160,7 @@ __int64 GetLastWriteTime(const CString &csFile);
 #define WM_SHOW_ERROR_MSG	WM_USER + 229
 #define WM_SHOW_ERROR_MSG	WM_USER + 229
 #define WM_RESTORE_DB	WM_USER + 230
 #define WM_RESTORE_DB	WM_USER + 230
 #define WM_BACKUP_DB	WM_USER + 231
 #define WM_BACKUP_DB	WM_USER + 231
+#define WM_REFRESH_FOOTER WM_USER + 232
 
 
 
 
 #if !defined(_BITSET_)
 #if !defined(_BITSET_)

+ 21 - 3
ScrollHelper.cpp

@@ -64,6 +64,22 @@ void CScrollHelper::SetDisplaySize(int displayWidth, int displayHeight, double z
 	int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
 	int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
 	int cySB = ::GetSystemMetrics(SM_CYHSCROLL);	
 	int cySB = ::GetSystemMetrics(SM_CYHSCROLL);	
 
 
+	CRect rect;
+	GetClientRectSB(m_attachWnd, rect);
+	double width = (rect.Width() * (1 / m_zoomScale)) + .5;
+	double height = (rect.Height() * (1 / m_zoomScale)) + .5;
+	CSize windowSize(width, height);
+
+	if (windowSize.cx >= displayWidth)
+	{
+		cxSB = 0;
+	}
+
+	if (windowSize.cy >= displayHeight)
+	{
+		cySB = 0;
+	}
+
 	//CString msg;
 	//CString msg;
 	//msg.Format(_T("width: %d, height: %d, scale: %f\r\n"), displayWidth, displayHeight, m_zoomScale);
 	//msg.Format(_T("width: %d, height: %d, scale: %f\r\n"), displayWidth, displayHeight, m_zoomScale);
 	//OutputDebugString(msg);
 	//OutputDebugString(msg);
@@ -277,7 +293,7 @@ BOOL CScrollHelper::Update(CPoint changes)
 	if (newYScrollPos > maxYScrollPos)
 	if (newYScrollPos > maxYScrollPos)
 		deltaYPos = maxYScrollPos - m_scrollPos.cy;
 		deltaYPos = maxYScrollPos - m_scrollPos.cy;
 
 
-	if (changes.y != 0)
+	if (deltaYPos != 0)
 	{
 	{
 		m_scrollPos.cy += deltaYPos;
 		m_scrollPos.cy += deltaYPos;
 		m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
 		m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
@@ -299,7 +315,7 @@ BOOL CScrollHelper::Update(CPoint changes)
 	if (newXScrollPos > maxXScrollPos)
 	if (newXScrollPos > maxXScrollPos)
 		deltaXPos = maxXScrollPos - m_scrollPos.cx;
 		deltaXPos = maxXScrollPos - m_scrollPos.cx;
 
 
-	if (changes.x != 0)
+	if (deltaXPos != 0)
 	{
 	{
 		m_scrollPos.cx += deltaXPos;
 		m_scrollPos.cx += deltaXPos;
 		m_attachWnd->SetScrollPos(SB_HORZ, m_scrollPos.cx, TRUE);
 		m_attachWnd->SetScrollPos(SB_HORZ, m_scrollPos.cx, TRUE);
@@ -440,7 +456,9 @@ void CScrollHelper::UpdateScrollInfo()
     // expect.
     // expect.
     CRect rect;
     CRect rect;
     GetClientRectSB(m_attachWnd, rect);
     GetClientRectSB(m_attachWnd, rect);
-    CSize windowSize(rect.Width() * (1/m_zoomScale), rect.Height() * (1/m_zoomScale));
+	double width = (rect.Width() * (1 / m_zoomScale)) + .5;
+	double height = (rect.Height() * (1 / m_zoomScale)) + .5;
+    CSize windowSize(width, height * (1/m_zoomScale));
 
 
     // Update horizontal scrollbar.
     // Update horizontal scrollbar.
     CSize deltaPos(0,0);
     CSize deltaPos(0,0);

+ 30 - 1
ToolTipEx.cpp

@@ -66,6 +66,7 @@ BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
 	ON_WM_MOVING()
 	ON_WM_MOVING()
 	ON_WM_ENTERSIZEMOVE()
 	ON_WM_ENTERSIZEMOVE()
 	ON_WM_HSCROLL()
 	ON_WM_HSCROLL()
+	ON_MESSAGE(WM_REFRESH_FOOTER, OnRefreshFooter)
 END_MESSAGE_MAP()
 END_MESSAGE_MAP()
 
 
 
 
@@ -144,7 +145,8 @@ BOOL CToolTipEx::Show(CPoint point)
 {
 {
     if(m_imageViewer.m_pGdiplusBitmap)
     if(m_imageViewer.m_pGdiplusBitmap)
     {
     {
-		m_clipData += _T(" | ") + StrF(_T("%d x %d"), m_imageViewer.m_pGdiplusBitmap->GetWidth(), m_imageViewer.m_pGdiplusBitmap->GetHeight());
+		int percent = (m_imageViewer.m_scale - 1.0) * 100.0;
+		m_clipData = m_originalClipData + _T(" | ") + StrF(_T("%d x %d, %d%%"), m_imageViewer.m_pGdiplusBitmap->GetWidth(), m_imageViewer.m_pGdiplusBitmap->GetHeight(), percent);
 
 
 		m_imageViewer.ShowWindow(SW_SHOW);
 		m_imageViewer.ShowWindow(SW_SHOW);
 
 
@@ -1437,4 +1439,31 @@ void CToolTipEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
 {
 {
 	int x = 9;
 	int x = 9;
 	//m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
 	//m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
+}
+
+LRESULT CToolTipEx::OnRefreshFooter(WPARAM wParam, LPARAM lParam)
+{
+	m_clipData = m_originalClipData;
+	if (m_imageViewer.m_pGdiplusBitmap)
+	{%
+		double round = -.5;
+		if (m_imageViewer.m_scale < 1)
+		{
+			round = .5;
+		}
+		int percent = ((m_imageViewer.m_scale) * 100.0) + .5;
+		/*CString scaleString;
+		if (percent != 0)
+		{
+			scaleString.Format(_T(", %d%%"), percent);
+		}*/
+		m_clipData = m_originalClipData + _T(" | ") + StrF(_T("%d x %d, %d%%"), m_imageViewer.m_pGdiplusBitmap->GetWidth(), m_imageViewer.m_pGdiplusBitmap->GetHeight(), percent);
+	}
+
+	m_clipDataStatic.SetWindowText(m_clipData);
+	m_clipDataStatic.Invalidate();
+
+	this->Invalidate();
+
+	return TRUE;
 }
 }

+ 3 - 1
ToolTipEx.h

@@ -41,7 +41,7 @@ public:
 
 
 	void SetSearchText(CString text) { m_searchText = text; }
 	void SetSearchText(CString text) { m_searchText = text; }
 
 
-	void SetClipData(CString data) { m_clipData = data; }
+	void SetClipData(CString data) { m_clipData = data; m_originalClipData = data; }
 	void SetFolderPath(CString path) { m_folderPath = path; }
 	void SetFolderPath(CString path) { m_folderPath = path; }
 
 
 	bool GetShowPersistant() { return m_showPersistant; }
 	bool GetShowPersistant() { return m_showPersistant; }
@@ -92,6 +92,7 @@ protected:
 	CGroupStatic m_clipDataStatic;
 	CGroupStatic m_clipDataStatic;
 	CGroupStatic m_folderPathStatic;
 	CGroupStatic m_folderPathStatic;
 	CString m_clipData;
 	CString m_clipData;
+	CString m_originalClipData;
 	CFont m_clipDataFont;
 	CFont m_clipDataFont;
 	bool m_saveWindowLockout;
 	bool m_saveWindowLockout;
 	int m_clipRow;
 	int m_clipRow;
@@ -143,4 +144,5 @@ public:
 	afx_msg void OnMoving(UINT fwSide, LPRECT pRect);
 	afx_msg void OnMoving(UINT fwSide, LPRECT pRect);
 	afx_msg void OnEnterSizeMove();
 	afx_msg void OnEnterSizeMove();
 	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
+	afx_msg LRESULT OnRefreshFooter(WPARAM wParam, LPARAM lParam);
 };
 };