ImageViewer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // ImageViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "ImageViewer.h"
  6. #include "BitmapHelper.h"
  7. // CImageViewer
  8. IMPLEMENT_DYNAMIC(CImageViewer, CWnd)
  9. CImageViewer::CImageViewer()
  10. {
  11. m_pBitmap = NULL;
  12. m_scrollHelper.AttachWnd(this);
  13. m_hoveringOverImage = false;
  14. }
  15. CImageViewer::~CImageViewer()
  16. {
  17. }
  18. BEGIN_MESSAGE_MAP(CImageViewer, CWnd)
  19. ON_WM_HSCROLL()
  20. ON_WM_VSCROLL()
  21. ON_WM_MOUSEWHEEL()
  22. ON_WM_PAINT()
  23. ON_WM_SIZE()
  24. ON_WM_SETCURSOR()
  25. ON_WM_LBUTTONUP()
  26. END_MESSAGE_MAP()
  27. BOOL CImageViewer::Create(CWnd* pParent)
  28. {
  29. BOOL bSuccess;
  30. // Register window class
  31. CString csClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
  32. LoadCursor(NULL, IDC_ARROW),
  33. CBrush(::GetSysColor(COLOR_BTNFACE)));
  34. // If no parent supplied then try and get a pointer to it anyway
  35. if (!pParent)
  36. pParent = AfxGetMainWnd();
  37. // Create popup window
  38. //bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, // Extended style
  39. bSuccess = CreateEx(0,
  40. csClassName, // Classname
  41. _T(""), // Title
  42. WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, // style
  43. 0, 0, // position - updated soon.
  44. 390, 130, // Size - updated soon
  45. pParent->GetSafeHwnd(), // handle to parent
  46. 0, // No menu
  47. NULL);
  48. if (!bSuccess) return FALSE;
  49. return TRUE;
  50. }
  51. void CImageViewer::UpdateBitmapSize()
  52. {
  53. if (m_pBitmap != NULL)
  54. {
  55. if (CGetSetOptions::GetScaleImagesToDescWindow())
  56. {
  57. CRect rect;
  58. GetClientRect(rect);
  59. m_scrollHelper.SetDisplaySize(rect.Width(), rect.Height());
  60. }
  61. else
  62. {
  63. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  64. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  65. m_scrollHelper.SetDisplaySize(nWidth, nHeight);
  66. }
  67. }
  68. }
  69. void CImageViewer::OnPaint()
  70. {
  71. CPaintDC dc(this); // device context for painting
  72. CRect rect;
  73. GetClientRect(rect);
  74. CBrush Brush, *pOldBrush;
  75. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  76. pOldBrush = dc.SelectObject(&Brush);
  77. dc.FillRect(&rect, &Brush);
  78. if (m_pBitmap)
  79. {
  80. CDC MemDc;
  81. MemDc.CreateCompatibleDC(&dc);
  82. CBitmap *oldBitmap = MemDc.SelectObject(m_pBitmap);
  83. int width = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  84. int height = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  85. if (CGetSetOptions::GetScaleImagesToDescWindow())
  86. {
  87. double newWidth = rect.Width();
  88. double newHeight = rect.Height();
  89. if (width > 0 &&
  90. height > 0 &&
  91. rect.Width() > 0 &&
  92. rect.Height() > 0)
  93. {
  94. float origAspect = (width / (float)height);
  95. float newAspect = (rect.Width() / (float)rect.Height());
  96. if (origAspect > newAspect)
  97. {
  98. newHeight = (rect.Width() * height) / width;
  99. }
  100. else
  101. {
  102. newWidth = (rect.Height() * width) / height;
  103. }
  104. }
  105. dc.StretchBlt(rect.left, rect.top, newWidth, newHeight, &MemDc, 0, 0, width, height, SRCCOPY);
  106. //OutputDebugString(StrF(_T("scaling image, window size %d/%d, image %d/%d\n"), min(nWidth, rect.Width()), min(nHeight, rect.Height()), nWidth, nHeight));
  107. }
  108. else
  109. {
  110. CSize s = m_scrollHelper.GetScrollPos();
  111. dc.BitBlt(rect.left, rect.top, width, height, &MemDc, s.cx, s.cy, SRCCOPY);
  112. }
  113. //dc.StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), &MemDc, 0, 0, nWidth, nHeight, SRCCOPY);
  114. MemDc.SelectObject(oldBitmap);
  115. rect.top += height;
  116. }
  117. // Cleanup
  118. dc.SelectObject(pOldBrush);
  119. }
  120. void CImageViewer::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  121. {
  122. m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
  123. }
  124. void CImageViewer::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  125. {
  126. m_scrollHelper.OnVScroll(nSBCode, nPos, pScrollBar);
  127. }
  128. BOOL CImageViewer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  129. {
  130. BOOL wasScrolled = m_scrollHelper.OnMouseWheel(nFlags, zDelta, pt);
  131. return wasScrolled;
  132. }
  133. void CImageViewer::OnSize(UINT nType, int cx, int cy)
  134. {
  135. CWnd::OnSize(nType, cx, cy);
  136. m_scrollHelper.OnSize(nType, cx, cy);
  137. }
  138. BOOL CImageViewer::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  139. {
  140. if (this->m_pBitmap &&
  141. pWnd->m_hWnd == this->m_hWnd &&
  142. nHitTest == HTCLIENT)
  143. {
  144. if (CGetSetOptions::GetScaleImagesToDescWindow())
  145. {
  146. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_IN));
  147. }
  148. else
  149. {
  150. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_OUT));
  151. }
  152. m_hoveringOverImage = true;
  153. return TRUE;
  154. }
  155. m_hoveringOverImage = false;
  156. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  157. }
  158. void CImageViewer::OnLButtonUp(UINT nFlags, CPoint point)
  159. {
  160. if (this->m_pBitmap &&
  161. m_hoveringOverImage)
  162. {
  163. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  164. this->UpdateBitmapSize();
  165. Invalidate();
  166. return;
  167. }
  168. CWnd::OnLButtonUp(nFlags, point);
  169. }