ImageViewer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // ImageViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "ImageViewer.h"
  6. #include "BitmapHelper.h"
  7. #include "memdc.h"
  8. // CImageViewer
  9. IMPLEMENT_DYNAMIC(CImageViewer, CWnd)
  10. CImageViewer::CImageViewer()
  11. {
  12. m_scrollHelper.AttachWnd(this);
  13. m_hoveringOverImage = false;
  14. m_pGdiplusBitmap = NULL;
  15. }
  16. CImageViewer::~CImageViewer()
  17. {
  18. delete m_pGdiplusBitmap;
  19. }
  20. BEGIN_MESSAGE_MAP(CImageViewer, CWnd)
  21. ON_WM_HSCROLL()
  22. ON_WM_VSCROLL()
  23. ON_WM_MOUSEWHEEL()
  24. ON_WM_PAINT()
  25. ON_WM_SIZE()
  26. ON_WM_SETCURSOR()
  27. ON_WM_LBUTTONUP()
  28. ON_WM_MOUSEHWHEEL()
  29. ON_WM_ERASEBKGND()
  30. ON_MESSAGE(WM_GESTURE, &CImageViewer::OnGesture)
  31. ON_MESSAGE(WM_GESTURENOTIFY, &CImageViewer::OnGestureNotify)
  32. END_MESSAGE_MAP()
  33. BOOL CImageViewer::Create(CWnd* pParent)
  34. {
  35. BOOL bSuccess;
  36. // Register window class
  37. CString csClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
  38. LoadCursor(NULL, IDC_ARROW),
  39. CBrush(::GetSysColor(COLOR_BTNFACE)));
  40. // If no parent supplied then try and get a pointer to it anyway
  41. if (!pParent)
  42. pParent = AfxGetMainWnd();
  43. // Create popup window
  44. //bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, // Extended style
  45. bSuccess = CreateEx(0,
  46. csClassName, // Classname
  47. _T(""), // Title
  48. WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, // style
  49. 0, 0, // position - updated soon.
  50. 390, 130, // Size - updated soon
  51. pParent->GetSafeHwnd(), // handle to parent
  52. 0, // No menu
  53. NULL);
  54. if (!bSuccess) return FALSE;
  55. return TRUE;
  56. }
  57. void CImageViewer::UpdateBitmapSize()
  58. {
  59. if (m_pGdiplusBitmap != NULL)
  60. {
  61. if (CGetSetOptions::GetScaleImagesToDescWindow())
  62. {
  63. CRect rect;
  64. GetClientRect(rect);
  65. m_scrollHelper.SetDisplaySize(0, 0);
  66. m_scrollHelper.DetachWnd();
  67. }
  68. else
  69. {
  70. m_scrollHelper.AttachWnd(this);
  71. m_scrollHelper.SetDisplaySize(m_pGdiplusBitmap->GetWidth(), m_pGdiplusBitmap->GetHeight());
  72. }
  73. }
  74. }
  75. void CImageViewer::OnPaint()
  76. {
  77. CPaintDC dc(this); // device context for painting
  78. CMemDCEx memDC(&dc);
  79. CRect rect;
  80. GetClientRect(rect);
  81. CBrush Brush, *pOldBrush;
  82. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  83. pOldBrush = memDC.SelectObject(&Brush);
  84. memDC.FillRect(&rect, &Brush);
  85. if (m_pGdiplusBitmap)
  86. {
  87. int width = m_pGdiplusBitmap->GetWidth();
  88. int height = m_pGdiplusBitmap->GetHeight();
  89. if (CGetSetOptions::GetScaleImagesToDescWindow())
  90. {
  91. double newWidth = rect.Width();
  92. double newHeight = rect.Height();
  93. if (width > 0 &&
  94. height > 0 &&
  95. rect.Width() > 0 &&
  96. rect.Height() > 0)
  97. {
  98. float origAspect = (width / (float)height);
  99. float newAspect = (rect.Width() / (float)rect.Height());
  100. if (origAspect > newAspect)
  101. {
  102. newHeight = (rect.Width() * height) / width;
  103. }
  104. else
  105. {
  106. newWidth = (rect.Height() * width) / height;
  107. }
  108. }
  109. Gdiplus::ImageAttributes attrs;
  110. Gdiplus::Rect dest(0, 0, (int)newWidth, (int)newHeight);
  111. Gdiplus::Graphics graphics(memDC);
  112. graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
  113. graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
  114. graphics.DrawImage(m_pGdiplusBitmap, dest, 0, 0, width, height, Gdiplus::UnitPixel, &attrs);
  115. }
  116. else
  117. {
  118. CSize s = m_scrollHelper.GetScrollPos();
  119. Gdiplus::Graphics graphics(memDC);
  120. graphics.DrawImage(m_pGdiplusBitmap, rect.left, rect.top, s.cx, s.cy, width, height, Gdiplus::UnitPixel);
  121. }
  122. rect.top += height;
  123. }
  124. memDC.SelectObject(pOldBrush);
  125. }
  126. void CImageViewer::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  127. {
  128. //OutputDebugString(_T("OnHScroll\r\n"));
  129. m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
  130. }
  131. void CImageViewer::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  132. {
  133. //OutputDebugString(_T("OnVScroll\r\n"));
  134. m_scrollHelper.OnVScroll(nSBCode, nPos, pScrollBar);
  135. }
  136. BOOL CImageViewer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  137. {
  138. //OutputDebugString(_T("OnMouseWheel\r\n"));
  139. BOOL wasScrolled = m_scrollHelper.OnMouseWheel(nFlags, zDelta, pt);
  140. return wasScrolled;
  141. }
  142. void CImageViewer::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
  143. {
  144. //OutputDebugString(_T("OnMouseHWheel\r\n"));
  145. BOOL wasScrolled = m_scrollHelper.OnMouseHWheel(nFlags, -zDelta, pt);
  146. CWnd::OnMouseHWheel(nFlags, zDelta, pt);
  147. }
  148. void CImageViewer::OnSize(UINT nType, int cx, int cy)
  149. {
  150. CWnd::OnSize(nType, cx, cy);
  151. m_scrollHelper.OnSize(nType, cx, cy);
  152. }
  153. BOOL CImageViewer::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  154. {
  155. if (this->m_pGdiplusBitmap &&
  156. pWnd->m_hWnd == this->m_hWnd &&
  157. nHitTest == HTCLIENT)
  158. {
  159. if (CGetSetOptions::GetScaleImagesToDescWindow())
  160. {
  161. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_IN));
  162. }
  163. else
  164. {
  165. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_OUT));
  166. }
  167. m_hoveringOverImage = true;
  168. return TRUE;
  169. }
  170. m_hoveringOverImage = false;
  171. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  172. }
  173. void CImageViewer::OnLButtonUp(UINT nFlags, CPoint point)
  174. {
  175. if (this->m_pGdiplusBitmap &&
  176. m_hoveringOverImage)
  177. {
  178. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  179. this->UpdateBitmapSize();
  180. Invalidate();
  181. return;
  182. }
  183. CWnd::OnLButtonUp(nFlags, point);
  184. }
  185. BOOL CImageViewer::OnEraseBkgnd(CDC* pDC)
  186. {
  187. //OutputDebugString(_T("image viewer OnEraseBkgnd\r\n"));
  188. //return CWnd::OnEraseBkgnd(pDC);
  189. return FALSE;
  190. }
  191. LRESULT CImageViewer::OnGesture(WPARAM wParam, LPARAM lParam)
  192. {
  193. CPoint ptZoomCenter;
  194. double k;
  195. GESTUREINFO gi;
  196. ZeroMemory(&gi, sizeof(GESTUREINFO));
  197. gi.cbSize = sizeof(GESTUREINFO);
  198. BOOL bResult = GetGestureInfo((HGESTUREINFO)lParam, &gi);
  199. BOOL bHandled = FALSE;
  200. if (bResult) {
  201. // now interpret the gesture
  202. switch (gi.dwID) {
  203. case GID_ZOOM:
  204. //OutputDebugString(_T("zoom\r\n"));
  205. // Code for zooming goes here
  206. bHandled = TRUE;
  207. switch (gi.dwFlags)
  208. {
  209. case GF_BEGIN:
  210. m_dwArguments = LODWORD(gi.ullArguments);
  211. m_ptFirst.x = gi.ptsLocation.x;
  212. m_ptFirst.y = gi.ptsLocation.y;
  213. ::ScreenToClient(m_hWnd, &m_ptFirst);
  214. break;
  215. default:
  216. // We read here the second point of the gesture. This is middle point between
  217. // fingers in this new position.
  218. m_ptSecond.x = gi.ptsLocation.x;
  219. m_ptSecond.y = gi.ptsLocation.y;
  220. ::ScreenToClient(m_hWnd, &m_ptSecond);
  221. // We have to calculate zoom center point
  222. ptZoomCenter.x = (m_ptFirst.x + m_ptSecond.x) / 2;
  223. ptZoomCenter.y = (m_ptFirst.y + m_ptSecond.y) / 2;
  224. // The zoom factor is the ratio between the new and the old distance.
  225. // The new distance between two fingers is stored in gi.ullArguments
  226. // (lower DWORD) and the old distance is stored in _dwArguments.
  227. k = (double)(LODWORD(gi.ullArguments)) / (double)(m_dwArguments);
  228. // Now we process zooming in/out of the object
  229. //ProcessZoom(k, ptZoomCenter.x, ptZoomCenter.y);
  230. //m_scrollHelper.Update(ptZoomCenter);
  231. //CString cs;
  232. //cs.Format(_T("ZOOM k: %f, x: %d, y: %d\r\n"), k, ptZoomCenter.x, ptZoomCenter.y);
  233. //OutputDebugString(cs);
  234. //InvalidateRect(hWnd, NULL, TRUE);
  235. // Now we have to store new information as a starting information
  236. // for the next step in this gesture.
  237. m_ptFirst = m_ptSecond;
  238. m_dwArguments = LODWORD(gi.ullArguments);
  239. break;
  240. }
  241. break;
  242. case GID_PAN:
  243. //OutputDebugString(_T("pan\r\n"));
  244. // Code for panning goes here
  245. bHandled = TRUE;
  246. switch (gi.dwFlags)
  247. {
  248. case GF_BEGIN:
  249. m_ptFirst.x = gi.ptsLocation.x;
  250. m_ptFirst.y = gi.ptsLocation.y;
  251. ::ScreenToClient(m_hWnd, &m_ptFirst);
  252. break;
  253. default:
  254. // We read the second point of this gesture. It is a middle point
  255. // between fingers in this new position
  256. m_ptSecond.x = gi.ptsLocation.x;
  257. m_ptSecond.y = gi.ptsLocation.y;
  258. ::ScreenToClient(m_hWnd, &m_ptSecond);
  259. int xDiff = m_ptSecond.x - m_ptFirst.x;
  260. int yDiff = m_ptSecond.y - m_ptFirst.y;
  261. m_scrollHelper.Update(CPoint(-xDiff, -yDiff));
  262. //CString cs;
  263. //cs.Format(_T("x: %d, y: %d\r\n"), xDiff, yDiff);
  264. //OutputDebugString(cs);
  265. // We apply move operation of the object
  266. //ProcessMove(_ptSecond.x - _ptFirst.x, _ptSecond.y - _ptFirst.y);
  267. //InvalidateRect(hWnd, NULL, TRUE);
  268. // We have to copy second point into first one to prepare
  269. // for the next step of this gesture.
  270. m_ptFirst = m_ptSecond;
  271. break;
  272. }
  273. break;
  274. break;
  275. case GID_ROTATE:
  276. //OutputDebugString(_T("rotate\r\n"));
  277. // Code for rotation goes here
  278. bHandled = TRUE;
  279. break;
  280. case GID_TWOFINGERTAP:
  281. //OutputDebugString(_T("two finger\r\n"));
  282. // Code for two-finger tap goes here
  283. bHandled = TRUE;
  284. break;
  285. case GID_PRESSANDTAP:
  286. //OutputDebugString(_T("press and tap\r\n"));
  287. // Code for roll over goes here
  288. bHandled = TRUE;
  289. break;
  290. default:
  291. // A gesture was not recognized
  292. break;
  293. }
  294. }
  295. else {
  296. DWORD dwErr = GetLastError();
  297. if (dwErr > 0) {
  298. //MessageBoxW(hWnd, L"Error!", L"Could not retrieve a GESTUREINFO structure.", MB_OK);
  299. }
  300. }
  301. return FALSE;
  302. }
  303. LRESULT CImageViewer::OnGestureNotify(WPARAM wParam, LPARAM lParam)
  304. {
  305. // This is the right place to define the list of gestures that this
  306. // application will support. By populating GESTURECONFIG structure
  307. // and calling SetGestureConfig function. We can choose gestures
  308. // that we want to handle in our application. In this app we
  309. // decide to handle all gestures.
  310. GESTURECONFIG gc = {
  311. GID_PAN, // gesture ID
  312. GC_PAN | GC_PAN_WITH_SINGLE_FINGER_VERTICALLY | GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY | GC_PAN_WITH_GUTTER | GC_PAN_WITH_INERTIA, // settings related to gesture ID that are to be
  313. // turned on
  314. 0 // settings related to gesture ID that are to be
  315. // turned off
  316. };
  317. BOOL bResult = ::SetGestureConfig(
  318. m_hWnd, // window for which configuration is specified
  319. 0, // reserved, must be 0
  320. 1, // count of GESTURECONFIG structures
  321. &gc, // array of GESTURECONFIG structures, dwIDs will be processed in the
  322. // order specified and repeated occurances will overwrite previous ones
  323. sizeof(GESTURECONFIG) // sizeof(GESTURECONFIG)
  324. );
  325. return TRUE;
  326. }