ColourPicker.cpp
上传用户:cn05999
上传日期:2020-06-29
资源大小:84k
文件大小:6k
源码类别:

Static控件

开发平台:

Visual C++

  1. // ColourPicker.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CProgressCtrl.h"
  5. #include "ColourPicker.h"
  6. //#include "math.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CColourPicker
  14. IMPLEMENT_DYNCREATE(CColourPicker,CButton)
  15. CColourPicker::CColourPicker()
  16. {
  17. SetBkColour(GetSysColor(COLOR_3DFACE));
  18. SetTextColour(GetSysColor(COLOR_BTNTEXT));
  19. m_bTrackSelection=FALSE;
  20. m_nSelectionMode=CP_MODE_BK;
  21. m_bActive=FALSE;
  22. }
  23. CColourPicker::~CColourPicker()
  24. {
  25. }
  26. BEGIN_MESSAGE_MAP(CColourPicker, CButton)
  27. //{{AFX_MSG_MAP(CColourPicker)
  28. ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
  29. ON_WM_CREATE()
  30. //}}AFX_MSG_MAP
  31. ON_MESSAGE(CPN_SELENDOK, OnSelEndOK)
  32. ON_MESSAGE(CPN_SELENDCANCEL,OnSelEndCancel)
  33. ON_MESSAGE(CPN_SELCHANGE, OnSelChange)
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CColourPicker message handlers
  37. void AFXAPI DDX_ColourPicker(CDataExchange* pDX,int nIDC,COLORREF& crColour)
  38. {
  39. //得到非编辑控件CColourPicker的窗体句柄
  40. HWND hWndCtrl=pDX->PrepareCtrl(nIDC);
  41. ASSERT(hWndCtrl!=NULL);
  42. //由窗体句柄得到CColourPicker的窗体句柄
  43. CColourPicker *pColourPicker=(CColourPicker* )CWnd::FromHandle(hWndCtrl);
  44. //由变量m_bSaveandValideate 的指示状态来保存和获取颜色值
  45. if(pDX->m_bSaveAndValidate)
  46. {
  47. crColour=pColourPicker->GetColour();
  48. }
  49. else
  50. {
  51. pColourPicker->SetColour(crColour);
  52. }
  53. }
  54. int CColourPicker::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  55. {
  56. if (CButton::OnCreate(lpCreateStruct) == -1)
  57. return -1;
  58. // TODO: Add your specialized creation code here
  59. SetWindowSize();
  60. return 0;
  61. }
  62. void CColourPicker::OnClicked() 
  63. {
  64. // TODO: Add your control notification handler code here
  65. m_bActive=TRUE;
  66. CRect rect;
  67. GetWindowRect(rect);
  68. new CColourPopup(CPoint(rect.left,rect.bottom),GetColour(),this,10,_T("其它颜色…"));
  69. return ;
  70. }
  71. void CColourPicker::SetBkColour(COLORREF crColourBk)
  72. {
  73. m_crColourBk=crColourBk;
  74. if(IsWindow(m_hWnd))
  75. RedrawWindow();
  76. }
  77. void CColourPicker::SetTextColour(COLORREF crColourText)
  78. {
  79. m_crColourText=crColourText;
  80. if(IsWindow(m_hWnd))
  81. RedrawWindow();
  82. }
  83. void CColourPicker::SetWindowSize()
  84. {
  85. //得到边界的宽度
  86. CSize MarginSize(::GetSystemMetrics(SM_CXEDGE),::GetSystemMetrics(SM_CYEDGE));
  87. //得到下拉箭头的大小尺寸
  88. int nArrowWidth=max(::GetSystemMetrics(SM_CXHTHUMB),5*MarginSize.cx);
  89. int nArrowHeight=max(::GetSystemMetrics(SM_CYVTHUMB),5*MarginSize.cy);
  90. CSize ArrowSize(max(nArrowWidth,nArrowHeight),max(nArrowWidth,nArrowHeight));
  91. //得到窗体的大小
  92. CRect rect;
  93. GetWindowRect(rect);
  94. CWnd* pParent=GetParent();
  95. if(pParent)
  96. //将rect矩形坐标转换为客户区坐标系统
  97. //即以对话框窗体左上角为坐标参考点(0,0)
  98. pParent->ScreenToClient(rect);
  99. //将窗体设置的足够大
  100. int nWidth=max(rect.Width(),2*ArrowSize.cx+2*MarginSize.cx);
  101. MoveWindow(rect.left,rect.top,nWidth,ArrowSize.cy+2*MarginSize.cy,TRUE);
  102. GetWindowRect(rect);
  103. ScreenToClient(rect);
  104. //设定下拉箭 头区域的位置
  105. m_ArrowRect.SetRect(rect.right-ArrowSize.cx-MarginSize.cx,
  106. rect.top+MarginSize.cy,rect.right-MarginSize.cx,
  107. rect.bottom-MarginSize.cy);
  108. }
  109. LONG CColourPicker::OnSelChange(UINT lParam,LONG wParam)
  110. {
  111. if(m_bTrackSelection)
  112. SetColour((COLORREF) lParam);
  113. return TRUE;
  114. }
  115. LONG CColourPicker::OnSelEndCancel(UINT lParam,LONG wParam)
  116. {
  117. m_bActive =FALSE;
  118. SetColour((COLORREF) lParam);
  119. CWnd* pParent=GetParent();
  120. return TRUE;
  121. }
  122. LONG CColourPicker::OnSelEndOK(UINT lParam,LONG wParam)
  123. {
  124. COLORREF crNewColour=(COLORREF) lParam;
  125. m_bActive=FALSE;
  126. SetColour(crNewColour);
  127. CWnd* pParent=GetParent();
  128. if(crNewColour!=GetColour())
  129. if(pParent) pParent->SendMessage(CPN_SELCHANGE,lParam,(WPARAM)GetDlgCtrlID());
  130. return TRUE;
  131. }
  132. void CColourPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  133. {
  134. // TODO: Add your code to draw the specified item
  135. ASSERT(lpDrawItemStruct);
  136. CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  137. CRect rect =lpDrawItemStruct->rcItem;
  138. UINT  state =lpDrawItemStruct->itemState;
  139. DWORD dwStyle=GetStyle();
  140. CSize Margins(::GetSystemMetrics(SM_CXEDGE),::GetSystemMetrics(SM_CYEDGE));
  141. //绘制下拉箭头
  142. if(m_bActive)
  143. state=ODS_SELECTED;
  144. pDC->DrawFrameControl(&m_ArrowRect,DFC_SCROLL,DFCS_SCROLLDOWN|
  145. ((state&ODS_SELECTED)?DFCS_PUSHED:0)|
  146. ((state&ODS_DISABLED)?DFCS_INACTIVE:0));
  147. pDC->DrawEdge(rect,EDGE_SUNKEN,BF_RECT);
  148. rect.DeflateRect(Margins.cx,Margins.cy);
  149. //除去下拉箭头外的区域并涂上颜色
  150. rect.right-=m_ArrowRect.Width();
  151. CBrush brush((state&ODS_DISABLED)?::GetSysColor(COLOR_3DFACE):m_crColourBk);
  152. CBrush* pOldBrush=(CBrush*)pDC->SelectObject(&brush);
  153. pDC->SelectStockObject(NULL_PEN);
  154. pDC->Rectangle(rect);
  155. pDC->SelectObject(pOldBrush);
  156. //写窗体文本
  157. GetWindowText(m_strText);
  158. if(m_strText.GetLength())
  159. {
  160. pDC->SetBkMode(TRANSPARENT);
  161. if(state&ODS_DISABLED)
  162. {
  163. rect.OffsetRect(1,1);
  164. pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
  165. pDC->DrawText(m_strText,rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  166. rect.OffsetRect(-1,-1);
  167. pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
  168. pDC->DrawText(m_strText,rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  169. }
  170. else
  171. {
  172. pDC->SetTextColor(m_crColourText);
  173. pDC->DrawText(m_strText,rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  174. }
  175. }
  176. //绘制选中的颜色拾取器
  177. if(state&ODS_FOCUS)
  178. {
  179. rect.DeflateRect(1,1);
  180. pDC->DrawFocusRect(rect);
  181. }
  182. }
  183. void CColourPicker::PreSubclassWindow() 
  184. {
  185. // TODO: Add your specialized code here and/or call the base class
  186. ModifyStyle(0,BS_OWNERDRAW);
  187. CButton::PreSubclassWindow();
  188. SetWindowSize();
  189. }