QRCodeViewer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // QRCodeViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "QRCodeViewer.h"
  6. #include "MainTableFunctions.h"
  7. #define TIMER_BUTTON_UP 1
  8. // QRCodeViewer
  9. IMPLEMENT_DYNAMIC(QRCodeViewer, CWnd)
  10. QRCodeViewer::QRCodeViewer()
  11. {
  12. m_descBackground = NULL;
  13. }
  14. QRCodeViewer::~QRCodeViewer()
  15. {
  16. if(m_descBackground != NULL)
  17. {
  18. DeleteObject(m_descBackground);
  19. }
  20. }
  21. BEGIN_MESSAGE_MAP(QRCodeViewer, CWnd)
  22. ON_WM_CREATE()
  23. ON_WM_PAINT()
  24. ON_WM_SIZE()
  25. ON_WM_NCHITTEST()
  26. ON_WM_NCPAINT()
  27. ON_WM_NCCALCSIZE()
  28. ON_WM_NCLBUTTONDOWN()
  29. ON_WM_NCMOUSEMOVE()
  30. ON_WM_NCLBUTTONUP()
  31. ON_WM_ERASEBKGND()
  32. ON_WM_CTLCOLOR()
  33. ON_WM_WINDOWPOSCHANGING()
  34. ON_WM_TIMER()
  35. END_MESSAGE_MAP()
  36. BOOL QRCodeViewer::CreateEx(CWnd *pParentWnd, unsigned char* bitmapData, int imageSize, CString desc, int rowHeight, LOGFONT logFont)
  37. {
  38. // Get the class name and create the window
  39. CString szClassName = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  40. m_bitmapData = bitmapData;
  41. m_imageSize = imageSize;
  42. m_descRowHeight = rowHeight;
  43. m_descBackground = CreateSolidBrush(RGB(255, 255, 255));
  44. if(CWnd::CreateEx(0, szClassName, _T(""), WS_POPUP, 0, 0, 0, 0, NULL, 0, NULL))
  45. {
  46. BOOL r = m_desc.Create(CMainTableFunctions::GetDisplayText(g_Opt.m_nLinesPerRow, desc), WS_CHILD|WS_VISIBLE, CRect(0,0,0,0), this, 2);
  47. m_font.CreateFontIndirect(&logFont);
  48. m_desc.SetFont(&m_font);
  49. m_DittoWindow.DoCreate(this);
  50. m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  51. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  52. m_DittoWindow.m_bDrawMinimize = false;
  53. m_DittoWindow.m_bDrawMaximize = true;
  54. m_DittoWindow.m_bDrawChevron = false;
  55. m_DittoWindow.m_sendWMClose = false;
  56. m_qrCodeDrawer.LoadRaw(m_bitmapData, m_imageSize);
  57. delete[] m_bitmapData;
  58. CRect parentRect;
  59. pParentWnd->GetWindowRect(&parentRect);
  60. CRect rect;
  61. rect.left = parentRect.left;
  62. rect.top = parentRect.top;
  63. rect.right = rect.left + m_DittoWindow.m_lLeftBorder + m_DittoWindow.m_lRightBorder + m_qrCodeDrawer.ImageWidth() + (CGetSetOptions::GetQRCodeBorderPixels() * 2);
  64. rect.bottom = rect.top + m_DittoWindow.m_lTopBorder + m_DittoWindow.m_lBottomBorder + rowHeight + 5 + m_qrCodeDrawer.ImageHeight() + (CGetSetOptions::GetQRCodeBorderPixels() * 2);
  65. CRect center = CenterRect(rect);
  66. EnsureWindowVisible(&center);
  67. ::MoveWindow(m_hWnd, center.left, center.top, center.Width(), center.Height(), TRUE);
  68. MoveControls();
  69. SetFocus();
  70. }
  71. else
  72. {
  73. delete[] m_bitmapData;
  74. }
  75. return TRUE;
  76. }
  77. void QRCodeViewer::OnSize(UINT nType, int cx, int cy)
  78. {
  79. CWnd::OnSize(nType, cx, cy);
  80. this->Invalidate();
  81. MoveControls();
  82. m_DittoWindow.DoSetRegion(this);
  83. }
  84. void QRCodeViewer::MoveControls()
  85. {
  86. CRect crRect;
  87. GetClientRect(crRect);
  88. int cx = crRect.Width();
  89. int cy = crRect.Height();
  90. if(m_desc.m_hWnd != NULL)
  91. {
  92. m_desc.MoveWindow(5, cy - m_descRowHeight - 5, cx - 10, m_descRowHeight);
  93. }
  94. }
  95. void QRCodeViewer::OnPaint()
  96. {
  97. CPaintDC dc(this);
  98. CRect thisRect;
  99. GetClientRect(thisRect);
  100. thisRect.bottom -= m_descRowHeight - 5;
  101. int width = thisRect.Width() - (CGetSetOptions::GetQRCodeBorderPixels() * 2);
  102. int height = min(width, (thisRect.Height() - (CGetSetOptions::GetQRCodeBorderPixels() * 2)));
  103. width = min(width, height);
  104. CRect imageRect(0, 0, width, height);
  105. CRect centerRect = CenterRectFromRect(imageRect, thisRect);
  106. m_qrCodeDrawer.Draw(&dc, this, centerRect.left, centerRect.top, false, false, width, height);
  107. }
  108. BOOL QRCodeViewer::PreTranslateMessage(MSG *pMsg)
  109. {
  110. m_DittoWindow.DoPreTranslateMessage(pMsg);
  111. switch(pMsg->message)
  112. {
  113. case WM_KEYDOWN:
  114. switch(pMsg->wParam)
  115. {
  116. case VK_ESCAPE:
  117. ::SendMessage(m_hWnd, WM_CLOSE, 0, 0);
  118. return TRUE;
  119. }
  120. }
  121. return CWnd::PreTranslateMessage(pMsg);
  122. }
  123. void QRCodeViewer::PostNcDestroy()
  124. {
  125. CWnd::PostNcDestroy();
  126. delete this;
  127. }
  128. void QRCodeViewer::OnNcPaint()
  129. {
  130. m_DittoWindow.DoNcPaint(this);
  131. }
  132. void QRCodeViewer::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  133. {
  134. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  135. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  136. }
  137. HITTEST_RET QRCodeViewer::OnNcHitTest(CPoint point)
  138. {
  139. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  140. if(Ret == -1)
  141. return CWnd::OnNcHitTest(point);
  142. return Ret;
  143. }
  144. BOOL QRCodeViewer::OnEraseBkgnd(CDC* pDC)
  145. {
  146. CRect rect;
  147. GetClientRect(&rect);
  148. CBrush myBrush(RGB(255, 255, 255));
  149. CBrush *pOld = pDC->SelectObject(&myBrush);
  150. BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
  151. pDC->SelectObject(pOld);
  152. return TRUE;
  153. }
  154. void QRCodeViewer::OnNcLButtonDown(UINT nHitTest, CPoint point)
  155. {
  156. int buttonPressed = m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  157. if (buttonPressed != 0)
  158. {
  159. SetTimer(TIMER_BUTTON_UP, 100, NULL);
  160. }
  161. CWnd::OnNcLButtonDown(nHitTest, point);
  162. }
  163. void QRCodeViewer::OnNcLButtonUp(UINT nHitTest, CPoint point)
  164. {
  165. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  166. switch(lRet)
  167. {
  168. case BUTTON_CLOSE:
  169. ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  170. break;
  171. }
  172. KillTimer(TIMER_BUTTON_UP);
  173. CWnd::OnNcLButtonUp(nHitTest, point);
  174. }
  175. void QRCodeViewer::OnNcMouseMove(UINT nHitTest, CPoint point)
  176. {
  177. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  178. CWnd::OnNcMouseMove(nHitTest, point);
  179. }
  180. HBRUSH QRCodeViewer::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  181. {
  182. HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor);
  183. if(pWnd->GetDlgCtrlID() == 2)
  184. {
  185. pDC->SetBkColor(RGB(255,255,255));
  186. return m_descBackground;
  187. }
  188. // TODO: Return a different brush if the default is not desired
  189. return hbr;
  190. }
  191. void QRCodeViewer::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  192. {
  193. CWnd::OnWindowPosChanging(lpwndpos);
  194. m_DittoWindow.SnapToEdge(this, lpwndpos);
  195. }
  196. void QRCodeViewer::OnTimer(UINT_PTR nIDEvent)
  197. {
  198. switch (nIDEvent)
  199. {
  200. case TIMER_BUTTON_UP:
  201. {
  202. if ((GetKeyState(VK_LBUTTON) & 0x100) == 0)
  203. {
  204. m_DittoWindow.DoNcLButtonUp(this, 0, CPoint(0, 0));
  205. KillTimer(TIMER_BUTTON_UP);
  206. }
  207. break;
  208. }
  209. }
  210. CWnd::OnTimer(nIDEvent);
  211. }