QRCodeViewer.cpp 5.3 KB

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