ImageViewer.cpp 11 KB

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