QRCodeViewer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 = false;
  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_qrCodeDrawer.ImageWidth() + m_DittoWindow.m_lLeftBorder + m_DittoWindow.m_lRightBorder;
  61. rect.bottom = rect.top + m_qrCodeDrawer.ImageHeight() + m_DittoWindow.m_lTopBorder + m_DittoWindow.m_lBottomBorder + rowHeight + 10;
  62. CRect center = CenterRect(rect);
  63. ::MoveWindow(m_hWnd, center.left, center.top, center.Width(), center.Height(), TRUE);
  64. MoveControls();
  65. SetFocus();
  66. }
  67. else
  68. {
  69. delete[] m_bitmapData;
  70. }
  71. return TRUE;
  72. }
  73. void QRCodeViewer::OnSize(UINT nType, int cx, int cy)
  74. {
  75. CWnd::OnSize(nType, cx, cy);
  76. MoveControls();
  77. m_DittoWindow.DoSetRegion(this);
  78. }
  79. void QRCodeViewer::MoveControls()
  80. {
  81. CRect crRect;
  82. GetClientRect(crRect);
  83. int cx = crRect.Width();
  84. int cy = crRect.Height();
  85. if(m_desc.m_hWnd != NULL)
  86. {
  87. m_desc.MoveWindow(5, cy - m_descRowHeight - 5, cx - 10, m_descRowHeight);
  88. }
  89. }
  90. void QRCodeViewer::OnPaint()
  91. {
  92. CPaintDC dc(this);
  93. m_qrCodeDrawer.Draw(&dc, this, 0, 0, false, false);
  94. }
  95. BOOL QRCodeViewer::PreTranslateMessage(MSG *pMsg)
  96. {
  97. m_DittoWindow.DoPreTranslateMessage(pMsg);
  98. switch(pMsg->message)
  99. {
  100. case WM_KEYDOWN:
  101. switch(pMsg->wParam)
  102. {
  103. case VK_ESCAPE:
  104. ::SendMessage(m_hWnd, WM_CLOSE, 0, 0);
  105. return TRUE;
  106. }
  107. }
  108. return CWnd::PreTranslateMessage(pMsg);
  109. }
  110. void QRCodeViewer::PostNcDestroy()
  111. {
  112. CWnd::PostNcDestroy();
  113. delete this;
  114. }
  115. void QRCodeViewer::OnNcPaint()
  116. {
  117. m_DittoWindow.DoNcPaint(this);
  118. }
  119. void QRCodeViewer::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  120. {
  121. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  122. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  123. }
  124. HITTEST_RET QRCodeViewer::OnNcHitTest(CPoint point)
  125. {
  126. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  127. if(Ret == -1)
  128. return CWnd::OnNcHitTest(point);
  129. return Ret;
  130. }
  131. BOOL QRCodeViewer::OnEraseBkgnd(CDC* pDC)
  132. {
  133. CRect rect;
  134. GetClientRect(&rect);
  135. CBrush myBrush(RGB(255, 255, 255));
  136. CBrush *pOld = pDC->SelectObject(&myBrush);
  137. BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
  138. pDC->SelectObject(pOld);
  139. return TRUE;
  140. }
  141. void QRCodeViewer::OnNcLButtonDown(UINT nHitTest, CPoint point)
  142. {
  143. m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  144. CWnd::OnNcLButtonDown(nHitTest, point);
  145. }
  146. void QRCodeViewer::OnNcLButtonUp(UINT nHitTest, CPoint point)
  147. {
  148. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  149. switch(lRet)
  150. {
  151. case BUTTON_CLOSE:
  152. ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  153. break;
  154. }
  155. CWnd::OnNcLButtonUp(nHitTest, point);
  156. }
  157. void QRCodeViewer::OnNcMouseMove(UINT nHitTest, CPoint point)
  158. {
  159. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  160. CWnd::OnNcMouseMove(nHitTest, point);
  161. }
  162. HBRUSH QRCodeViewer::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  163. {
  164. HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor);
  165. if(pWnd->GetDlgCtrlID() == 2)
  166. {
  167. pDC->SetBkColor(RGB(255,255,255));
  168. return m_descBackground;
  169. }
  170. // TODO: Return a different brush if the default is not desired
  171. return hbr;
  172. }