HColorBar.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // HColorBar.cpp : implementation file
  2. // Download by http://www.codefans.net
  3. #include "stdafx.h"
  4. #include "HDraw.h"
  5. #include "HColorBar.h"
  6. #include "HDrawDoc.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // HColorBar
  14. HColorBar::HColorBar()
  15. {
  16. //initiate colors
  17. int index = 0;
  18. for(int i = 0; i <= 256; i += 128){
  19. for(int j = 0; j <= 256; j += 128){
  20. for(int k = 0; k <= 256; k +=128){
  21. m_COLOR[index ++] = RGB(256==i?255:i, 256==j?255:j, 256==k?255:k);
  22. }
  23. }
  24. }
  25. m_COLOR[index] = RGB(1,2,3);
  26. }
  27. HColorBar::~HColorBar()
  28. {
  29. }
  30. BEGIN_MESSAGE_MAP(HColorBar, CToolBar)
  31. //{{AFX_MSG_MAP(HColorBar)
  32. ON_WM_PAINT()
  33. ON_WM_LBUTTONDOWN()
  34. ON_WM_LBUTTONDBLCLK()
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // HColorBar message handlers
  39. COLORREF HColorBar::GetColor()
  40. {
  41. return m_currentColor;
  42. }
  43. void HColorBar::OnPaint()
  44. {
  45. CPaintDC dc(this); // device context for painting
  46. // TODO: Add your message handler code here
  47. CRect rc;
  48. GetClientRect(&rc);
  49. //step1.1 draw color indicator frame
  50. dc.Draw3dRect(rc.left+1, rc.top+5, 33, 33, RGB(0, 0, 0), RGB(255, 255, 255));
  51. //step1.2 draw color indicator
  52. CPen pen2(PS_SOLID, 1, m_currentColor);
  53. CBrush bru2(m_currentColor);
  54. dc.SelectObject(&pen2);
  55. dc.SelectObject(&bru2);
  56. dc.Draw3dRect(rc.left+5, rc.top+5+5, 15, 15, RGB(255, 255, 255), RGB(150, 150, 150));
  57. dc.Rectangle(rc.left+6, rc.top+10, rc.left+29, rc.top+33);
  58. //step2.1
  59. //initiate color rects
  60. int i = 0;
  61. for( i=0;i<COLORNUMBER/2;i++)
  62. {
  63. m_rect[i*2].left=rc.left+36+i*(15+2)+1;
  64. m_rect[i*2].top=rc.top+5+1;
  65. m_rect[i*2].right=m_rect[i*2].left+13;
  66. m_rect[i*2].bottom=m_rect[i*2].top+13;
  67. m_rect[i*2+1].left=rc.left+36+i*(15+2)+1;
  68. m_rect[i*2+1].top=rc.top+22+1;
  69. m_rect[i*2+1].right=m_rect[i*2+1].left+13;
  70. m_rect[i*2+1].bottom=m_rect[i*2+1].top+13;
  71. };
  72. //step2.2
  73. //draw rects
  74. for(i =0;i<COLORNUMBER/2;i++)
  75. {
  76. dc.Draw3dRect(rc.left+36+i*(15+2), rc.top+5, 15, 15, RGB(0, 0, 0), RGB(255, 255, 255));
  77. CPen pen3(PS_SOLID, 1, m_COLOR[i*2]);
  78. CBrush bru3(m_COLOR[i*2]);
  79. dc.SelectObject(&pen3);
  80. dc.SelectObject(&bru3);
  81. dc.Rectangle(m_rect[i*2].left, m_rect[i*2].top, m_rect[i*2].right, m_rect[i*2].bottom);
  82. dc.Draw3dRect(rc.left+36+i*(15+2), rc.top+22, 15, 15, RGB(0, 0, 0), RGB(255, 255, 255));
  83. CPen pen4(PS_SOLID, 1, m_COLOR[i*2+1]);
  84. CBrush bru4(m_COLOR[i*2+1]);
  85. dc.SelectObject(&pen4);
  86. dc.SelectObject(&bru4);
  87. dc.Rectangle(m_rect[i*2+1].left, m_rect[i*2+1].top, m_rect[i*2+1].right, m_rect[i*2+1].bottom);
  88. };
  89. //step3 calculate total area
  90. m_rectAll.left=m_rect[0].left;
  91. m_rectAll.top=m_rect[0].top;
  92. m_rectAll.right=m_rect[COLORNUMBER-1].right;
  93. m_rectAll.bottom=m_rect[1].bottom;
  94. }
  95. void HColorBar::OnLButtonDown(UINT nFlags, CPoint point)
  96. {
  97. // TODO: Add your message handler code here and/or call default
  98. CClientDC dc(this);
  99. CRect rc;
  100. GetClientRect(&rc);
  101. for(int i=0;i<COLORNUMBER;i++)
  102. {
  103. if(m_rect[i].PtInRect(point))
  104. {
  105. //step1. change variable
  106. m_currentColor = m_COLOR[i];
  107. //step2. change document
  108. UpdateDocument();
  109. CPen pen(PS_SOLID, 1, m_currentColor);
  110. CBrush bru(m_currentColor);
  111. dc.SelectObject(&pen);
  112. dc.SelectObject(&bru);
  113. //step2. draw indicator
  114. dc.Rectangle(rc.left+6, rc.top+10, rc.left+29, rc.top+33);
  115. break;
  116. }
  117. }
  118. CToolBar::OnLButtonDown(nFlags, point);
  119. }
  120. void HColorBar::OnLButtonDblClk(UINT nFlags, CPoint point)
  121. {
  122. // TODO: Add your message handler code here and/or call default
  123. CClientDC dc(this);
  124. CRect rc;
  125. GetClientRect(&rc);
  126. if(m_rectAll.PtInRect(point))
  127. {
  128. CColorDialog dlg;
  129. dlg.m_cc.Flags |= CC_RGBINIT;
  130. dlg.m_cc.rgbResult=m_currentColor;
  131. if(dlg.DoModal()==IDOK)
  132. {
  133. //step1. set variable
  134. m_currentColor=dlg.GetColor();
  135. //step2. update document
  136. UpdateDocument();
  137. CPen pen(PS_SOLID, 1, m_currentColor);
  138. CBrush bru(m_currentColor);
  139. dc.SelectObject(&pen);
  140. dc.SelectObject(&bru);
  141. //step3. draw indicator
  142. dc.Rectangle(rc.left+6, rc.top+10, rc.left+19, rc.top+24);
  143. };
  144. }
  145. }
  146. void HColorBar::UpdateDocument()
  147. {
  148. //Get Document
  149. CHDrawDoc* pDoc = ((CHDrawDoc*)(((CMainFrame*)AfxGetMainWnd())->GetActiveDocument()));
  150. //Set color of document
  151. pDoc->m_picColor = m_currentColor;
  152. //Set color of selected strokes
  153. bool refresh = false;
  154. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  155. if(pDoc->m_strokeList.GetAt(i)->m_bSelected)
  156. pDoc->m_strokeList.GetAt(i)->m_penColor = m_currentColor;
  157. refresh = true;
  158. }
  159. if(refresh){
  160. pDoc->BackUp();
  161. ((CMainFrame*)AfxGetMainWnd())->GetActiveView()->Invalidate();
  162. }
  163. }