ColorPickerCB.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "ColorPickerCB.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. //  给颜色和名称结构数组赋值
  9. SColorAndName CColorPickerCB::ms_pColors[ CCB_MAX_COLORS ] =
  10. {
  11. SColorAndName( RGB( 0x00, 0x00, 0x00 ), "Black" ),
  12. SColorAndName( RGB( 0x80, 0x00, 0x00 ), "Maroon" ),
  13. SColorAndName( RGB( 0x00, 0x80, 0x00 ), "Green" ),
  14. SColorAndName( RGB( 0x80, 0x80, 0x00 ), "Olive" ),
  15. SColorAndName( RGB( 0x00, 0x00, 0x80 ), "Navy" ),
  16. SColorAndName( RGB( 0x80, 0x00, 0x80 ), "Purple" ),
  17. SColorAndName( RGB( 0x00, 0x80, 0x80 ), "Teal" ),
  18. SColorAndName( RGB( 0x80, 0x80, 0x80 ), "Grey" ),
  19. SColorAndName( RGB( 0xC0, 0xC0, 0xC0 ), "Silver" ),
  20. SColorAndName( RGB( 0xFF, 0x00, 0x00 ), "Red" ),
  21. SColorAndName( RGB( 0x00, 0xFF, 0x00 ), "Lime" ),
  22. SColorAndName( RGB( 0xFF, 0xFF, 0x00 ), "Yellow" ),
  23. SColorAndName( RGB( 0x00, 0x00, 0xFF ), "Blue" ),
  24. SColorAndName( RGB( 0xFF, 0x00, 0xFF ), "Fushcia" ),
  25. SColorAndName( RGB( 0x00, 0xFF, 0xFF ), "Aqua" ),
  26. SColorAndName( RGB( 0xFF, 0xFF, 0xFF ), "White" ),
  27. };
  28. CColorPickerCB::CColorPickerCB()
  29. {
  30. m_bInitialized = false;
  31. }
  32. CColorPickerCB::~CColorPickerCB()
  33. {
  34. }
  35. BEGIN_MESSAGE_MAP(CColorPickerCB, CComboBox)
  36. //{{AFX_MSG_MAP(CColorPickerCB)
  37. ON_WM_CREATE()
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CColorPickerCB message handlers
  42. int CColorPickerCB::OnCreate( LPCREATESTRUCT pCStruct ) 
  43. {
  44. if( CComboBox::OnCreate( pCStruct ) == -1 )
  45. return( -1 );
  46. return( 0 );
  47. }
  48. void CColorPickerCB::PreSubclassWindow() 
  49. {
  50. // Initialize Contents
  51. Initialize();
  52. // Subclass Control
  53. CComboBox::PreSubclassWindow();
  54. // Select First Item By Default
  55. SetCurSel( 0 );
  56. return;
  57. }
  58. //初始化列表框,将颜色值和名称赋给它
  59. void CColorPickerCB::Initialize( void )
  60. {
  61. int iAddedItem = -1;
  62. // If Already Initialized
  63. if( m_bInitialized )
  64. return;
  65. // For All Colors
  66. for( int iColor = 0; iColor < CCB_MAX_COLORS; iColor++ )
  67. {
  68. // Set Color Name/Text
  69. iAddedItem = AddString( ms_pColors[iColor].m_cColor );
  70. if( iAddedItem == CB_ERRSPACE )
  71. break;
  72. else
  73. // Set Color RGB Value
  74. SetItemData( iAddedItem, ms_pColors[
  75. iColor ].m_crColor );
  76. }
  77. // Set Initialized Flag
  78. m_bInitialized = true;
  79. }
  80. //按照列表项的颜色值和名称重画列表框
  81. void CColorPickerCB::DrawItem( LPDRAWITEMSTRUCT pDIStruct )
  82. {
  83. static CString sColor;
  84. CDC dcContext;
  85. CRect rItemRect( pDIStruct -> rcItem );
  86. CRect rBlockRect( rItemRect );
  87. CRect rTextRect( rBlockRect );
  88. CBrush brFrameBrush;
  89. int iFourthWidth = 0;
  90. int iItem = pDIStruct -> itemID;
  91. int iAction = pDIStruct -> itemAction;
  92. int iState = pDIStruct -> itemState;
  93. COLORREF crColor = 0;
  94. COLORREF crNormal = GetSysColor( COLOR_WINDOW );
  95. COLORREF crSelected = GetSysColor( COLOR_HIGHLIGHT );
  96. COLORREF crText = GetSysColor( COLOR_WINDOWTEXT );
  97. // 联系 CDC对象
  98. if( !dcContext.Attach( pDIStruct -> hDC ) )
  99. return;
  100. //获得1/4的列表项宽度
  101. iFourthWidth = ( rBlockRect.Width() / 4 );
  102. //创建黑色画刷
  103. brFrameBrush.CreateStockObject( BLACK_BRUSH );
  104. //如果该条目被选择
  105. if( iState & ODS_SELECTED )
  106. {
  107. dcContext.SetTextColor(
  108. ( 0x00FFFFFF & ~( crText ) ) );
  109. dcContext.SetBkColor( crSelected );
  110. dcContext.FillSolidRect( &rBlockRect, crSelected );
  111. }
  112. else//如果没有被选择
  113. {
  114. dcContext.SetTextColor( crText );
  115. dcContext.SetBkColor( crNormal );
  116. dcContext.FillSolidRect( &rBlockRect, crNormal );
  117. }
  118. //如果该项出于焦点状态
  119. if( iState & ODS_FOCUS )
  120. dcContext.DrawFocusRect( &rItemRect );
  121. //计算文字区域
  122. rTextRect.left += ( iFourthWidth + 2 );
  123. rTextRect.top += 2;
  124. //计算图像区域
  125. rBlockRect.DeflateRect( CSize( 2, 2 ) );
  126. rBlockRect.right = iFourthWidth;
  127. //显示文字(颜色名)
  128. if( iItem != -1 )
  129. {
  130. GetLBText( iItem, sColor );
  131. //如果处于不可用状态
  132. if( iState & ODS_DISABLED )
  133. {
  134. crColor = GetSysColor( COLOR_INACTIVECAPTIONTEXT );
  135. dcContext.SetTextColor( crColor );
  136. }
  137. else //如果处于一般状态
  138. crColor = GetItemData( iItem );
  139. dcContext.SetBkMode( TRANSPARENT );
  140. dcContext.TextOut( rTextRect.left, rTextRect.top,sColor );
  141. //填充颜色区域
  142. dcContext.FillSolidRect( &rBlockRect, crColor );
  143. //画边框
  144. dcContext.FrameRect( &rBlockRect, &brFrameBrush );
  145. }
  146. //分离CDC对象
  147. dcContext.Detach();
  148. }
  149. //获得选择项的颜色值
  150. COLORREF CColorPickerCB::GetSelectedColorValue( void )
  151. {
  152. //获得选择的条目索引
  153. int iSelectedItem = GetCurSel();
  154. //如果没有被选择
  155. if( iSelectedItem == CB_ERR )
  156. return( RGB( 0, 0, 0 ) ); // Return Black
  157. return( GetItemData( iSelectedItem ) );
  158. }
  159. //获得选择项的颜色名称
  160. CString CColorPickerCB::GetSelectedColorName( void )
  161. {
  162. //获得选择的条目索引
  163. int iSelectedItem = GetCurSel();
  164. //如果没有被选择
  165. if( iSelectedItem == CB_ERR )
  166. return( m_sColorName = afxEmptyString ); 
  167. GetLBText( iSelectedItem, m_sColorName );
  168. return( m_sColorName );
  169. }
  170. //设定指定颜色值对应的列表项的颜色值
  171. void CColorPickerCB::SetSelectedColorValue( COLORREF crClr )
  172. {
  173. int iItems = GetCount();
  174. for( int iItem = 0; iItem < iItems; iItem++ )
  175. {
  176. if( crClr == GetItemData( iItem ) )
  177. SetCurSel( iItem );
  178. }
  179. return;
  180. }
  181. //设定指定颜色名称对应列表项的名称
  182. void CColorPickerCB::SetSelectedColorName( PCSTR cpColor )
  183. {
  184. int iItems = GetCount();
  185. CString sColorName;
  186. for( int iItem = 0; iItem < iItems; iItem++ )
  187. {
  188. GetLBText( iItem, sColorName );
  189. if( !sColorName.CompareNoCase( cpColor ) )
  190. SetCurSel( iItem );
  191. }
  192. }
  193. //删除指定颜色名称对应的列表项
  194. bool CColorPickerCB::RemoveColor( PCSTR cpColor )
  195. {
  196. int iItems = GetCount();
  197. CString sColor;
  198. bool bRemoved = false;
  199. for( int iItem = 0; iItem < iItems; iItem++ )
  200. {
  201. GetLBText( iItem, sColor );
  202. if( !sColor.CompareNoCase( cpColor ) )
  203. {
  204. DeleteString( iItem );
  205. bRemoved = true;
  206. break;
  207. }
  208. }
  209. return( bRemoved );
  210. }
  211. //删除指定颜色值对应的列表项
  212. bool CColorPickerCB::RemoveColor( COLORREF crClr )
  213. {
  214. int iItems = GetCount();
  215. bool bRemoved = false;
  216. for( int iItem = 0; iItem < iItems; iItem++ )
  217. {
  218. if( crClr == GetItemData( iItem ) )
  219. {
  220. DeleteString( iItem );
  221. bRemoved = true;
  222. break;
  223. }
  224. }
  225. return( bRemoved );
  226. }
  227. //增加指定颜色名称和颜色值的列表项
  228. int CColorPickerCB::AddColor( PCSTR cpName, COLORREF crColor )
  229. {
  230. int iItem = -1;
  231. iItem = InsertString( -1, cpName );
  232. if( iItem != LB_ERR )
  233. SetItemData( iItem, crColor );
  234. return( iItem );
  235. }