ColorPicker.cpp
上传用户:juying163
上传日期:2014-09-24
资源大小:5867k
文件大小:16k
源码类别:

GIS编程

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////
  2. // ColorPicker.cpp : implementation file                      //
  3. //   //
  4. // Copyright 2001 WangJun   //
  5. // All Rights Reserved.   //
  6. //   //
  7. // Email: wangjun98@sohu.com   //
  8. // URL:   http://www.vckbase.com   //
  9. //   //
  10. // 1.0     2001/10/6   First release version.   //
  11. //   //
  12. ////////////////////////////////////////////////////////////////
  13. #include "stdafx.h"
  14. #include "ColorPicker.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #define LPARAM_X(lp)                        ((int)(short)LOWORD(lp))
  21. #define LPARAM_Y(lp)                        ((int)(short)HIWORD(lp))
  22. #define WM_SETCOLOR WM_USER+1
  23. #define WM_COLORDLG WM_USER+2
  24. #define IDC_COLORDLG_BUTTON 100
  25. CPen _pen3DDKShadow(PS_SOLID,1,::GetSysColor(COLOR_3DDKSHADOW));
  26. CPen _penW(PS_SOLID,1,RGB(0xff,0xff,0xff));
  27. CPen _penB(PS_SOLID,1,RGB(0,0,0));
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CColorPicker
  30. CColorPicker::CColorPicker()
  31. {
  32. const unsigned char pvANDPlaneC[]={0xff,0xf1,0xff,0xe0,0xff,0xc0,0xff,0x00,0xff,0x81,0xff,0x03,0xfe,0x07,0xfc,0x17,
  33. 0xf8,0x3f,0xf0,0x7f,0xe0,0xff,0xc1,0xff,0x83,0xff,0x87,0xff,0x0f,0xff,0x3f,0xff};
  34. const unsigned char pvXORPlaneC[]={0x00,0x00,0x00,0x0c,0x00,0x08,0x00,0x10,0x00,0x00,0x00,0x40,0x00,0xe0,0x01,0xc0,
  35. 0x03,0x80,0x07,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x30,0x00,0x40,0x00,0x00,0x00};
  36. int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
  37. int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
  38. m_hCursorStraw = NULL;
  39. if(cxCursor >=16 && cxCursor < 100 && cyCursor < 100 && cyCursor >= 16)
  40. {
  41. int size = cxCursor*cyCursor/8;
  42. unsigned char *pvANDPlane = new unsigned char[size];
  43. unsigned char *pvXORPlane = new unsigned char[size];
  44. if(pvANDPlane && pvXORPlane)
  45. {
  46. memset(pvANDPlane,0xff,size);
  47. memset(pvXORPlane,0x00,size);
  48. for(int j=0;j<16;j++)
  49. for(int i=0;i<2;i++)
  50. {
  51. *(pvANDPlane+j*cxCursor/8 + i) = *(pvANDPlaneC + j*2 + i);
  52. *(pvXORPlane+j*cxCursor/8 + i) = *(pvXORPlaneC + j*2 + i);
  53. }
  54. m_hCursorStraw = ::CreateCursor(::AfxGetInstanceHandle(),0,15,cxCursor,cyCursor,pvANDPlane,pvXORPlane);
  55. delete pvANDPlane;
  56. delete pvXORPlane;
  57. }
  58. }
  59. m_CurrentColor = COLORREF(::GetSysColor(COLOR_3DFACE));
  60. m_hwndBuddy = NULL;
  61. m_hPaletteWnd = NULL;
  62. m_bPaletteWndActive = FALSE;
  63. }
  64. CColorPicker::~CColorPicker()
  65. {
  66. ::DestroyCursor(m_hCursorStraw);
  67. }
  68. BEGIN_MESSAGE_MAP(CColorPicker, CButton)
  69. //{{AFX_MSG_MAP(CColorPicker)
  70. ON_WM_ERASEBKGND()
  71. ON_WM_LBUTTONDOWN()
  72. ON_WM_LBUTTONUP()
  73. ON_WM_SETCURSOR()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CColorPicker message handlers
  78. void CColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  79. {
  80. }
  81. BOOL CColorPicker::OnEraseBkgnd(CDC* pDC) 
  82. {
  83. CPen *pOldPen;
  84. COLORREF clr3DFace = GetSysColor(COLOR_3DFACE);
  85. CRect rect;
  86. GetClientRect(&rect);
  87. if(!m_bPaletteWndActive)
  88. {
  89. pDC->Draw3dRect (rect,RGB(0xff,0xff,0xff),GetSysColor (COLOR_3DDKSHADOW));
  90. rect.DeflateRect(1,1);
  91. pDC->Draw3dRect (rect,clr3DFace,clr3DFace);
  92. rect.InflateRect(1,1);
  93. }
  94. rect.DeflateRect(3,3);
  95. pDC->FillSolidRect(rect,m_CurrentColor);
  96. rect.left -= 1;rect.top -= 1;
  97. pOldPen = pDC->SelectObject(&_pen3DDKShadow);
  98. pDC->MoveTo(rect.right - 1,rect.top);
  99. pDC->LineTo(rect.left,rect.top);
  100. pDC->LineTo(rect.left,rect.bottom);
  101. pDC->SelectObject(&_penW);
  102. pDC->LineTo(rect.right - 6,rect.bottom);
  103. pDC->LineTo(rect.right - 6,rect.bottom - 4);
  104. pDC->LineTo(rect.right,rect.bottom - 4);
  105. pDC->MoveTo(rect.right,rect.top);
  106. pDC->LineTo(rect.right,rect.bottom - 3);
  107. rect.right += 1;
  108. rect.bottom += 1;
  109. rect.left = rect.right - 6;
  110. rect.top = rect.bottom - 4;
  111. pDC->FillSolidRect(rect,clr3DFace);
  112. pDC->SelectObject(&_penB);
  113. pDC->MoveTo(rect.left + 1,rect.top + 1);
  114. pDC->LineTo(rect.right,rect.top + 1);
  115. pDC->MoveTo(rect.left + 2,rect.top + 2);
  116. pDC->LineTo(rect.right-1,rect.top + 2);
  117. pDC->MoveTo(rect.left + 3,rect.top + 3);
  118. pDC->LineTo(rect.right-2,rect.top + 3);
  119. pDC->SelectObject(pOldPen);
  120. return TRUE;
  121. }
  122. void CColorPicker::OnLButtonDown(UINT nFlags, CPoint point) 
  123. {
  124. CButton::OnLButtonDown(nFlags, point);
  125. ::ClientToScreen(this->m_hWnd,&point);
  126. if(CreatePaletteWindow())
  127. {
  128. nFlags = (UINT)m_CurrentColor;
  129. ::PostMessage(m_hPaletteWnd,WM_LBUTTONDOWN,nFlags,MAKELPARAM(point.x,point.y));
  130. }
  131. HWND hwndParent = GetParent()->m_hWnd;
  132. if(hwndParent)
  133. {
  134. POINT p1,p2;
  135. RECT rect;
  136. ::GetWindowRect(m_hWnd,&rect);
  137. p1.x = rect.left;
  138. p1.y = rect.top;
  139. p2.x = rect.right;
  140. p2.y = rect.bottom;
  141. ::ScreenToClient(hwndParent,&p1);
  142. ::ScreenToClient(hwndParent,&p2);
  143. rect.left = p1.x;
  144. rect.top = p1.y;
  145. rect.right = p2.x;
  146. rect.bottom = p2.y;
  147. ::InvalidateRect(hwndParent,&rect,TRUE);
  148. }
  149. }
  150. void CColorPicker::OnLButtonUp(UINT nFlags, CPoint point) 
  151. {
  152. DestroyPaletteWindow();
  153. if((COLORREF)nFlags != m_CurrentColor)
  154. SetColor((COLORREF)nFlags);
  155. else
  156. Invalidate();
  157. }
  158. LONG FAR PASCAL CColorPicker::PaletteWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  159. {
  160. HDC hDC;
  161. PAINTSTRUCT ps;
  162. POINT point;
  163. CRect crtColorRect(5,125,40,143);
  164. static POINT oldPoint;
  165. static BOOL bMouseMoved = FALSE;
  166. static HWND hwndParent;
  167. static HWND hwndColorBtn;
  168. static HFONT hFont = NULL;
  169. static COLORREF crtColor,oldColor;
  170. static int iActiveRect = -1;
  171. switch(nMsg)
  172. {
  173. case WM_CREATE:
  174. {
  175. hwndParent = ((LPCREATESTRUCT)lParam)->hwndParent;
  176. crtColor = ::GetSysColor(COLOR_3DFACE);
  177. LOGFONT logFont;
  178. ZeroMemory((void*)&logFont,sizeof(logFont));
  179. strcpy(logFont.lfFaceName,"宋体");
  180. logFont.lfHeight = -12;
  181. logFont.lfWeight = 400;
  182. logFont.lfCharSet = GB2312_CHARSET;
  183. logFont.lfOutPrecision = 3;
  184. logFont.lfClipPrecision = 2; 
  185. logFont.lfQuality = 1;
  186. logFont.lfPitchAndFamily = 2;
  187. hFont = ::CreateFontIndirect(&logFont);
  188. ////色彩对话框按钮
  189. hwndColorBtn = ::CreateWindow("BUTTON","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,167,126,16,16,hWnd,HMENU(IDC_COLORDLG_BUTTON),::AfxGetInstanceHandle(),NULL);
  190. if(hwndColorBtn)
  191. ::ShowWindow(hwndColorBtn,SW_SHOW);
  192. }
  193. break;
  194. case WM_DESTROY:
  195. ::DeleteObject(hFont);
  196. break;
  197. case WM_DRAWITEM:
  198. {
  199. LPDRAWITEMSTRUCT lpDis = (LPDRAWITEMSTRUCT)lParam;
  200. if(lpDis->CtlID == IDC_COLORDLG_BUTTON)
  201. {
  202. CRect rect(3,3,8,8);
  203. ::FillRect(lpDis->hDC,&lpDis->rcItem,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
  204. HPEN hOldPen = (HPEN)::SelectObject(lpDis->hDC,_penW);
  205. if(lpDis->itemState == 17)
  206. {
  207. ::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
  208. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
  209. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
  210. ::SelectObject(lpDis->hDC,_penB);
  211. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
  212. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
  213. rect.OffsetRect(1,1);
  214. }
  215. else
  216. {
  217. ::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
  218. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
  219. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
  220. ::SelectObject(lpDis->hDC,_penB);
  221. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
  222. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
  223. }
  224. ::SelectObject(lpDis->hDC,_pen3DDKShadow);
  225. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  226. ::SetBkColor(lpDis->hDC, RGB(0xff,0,0));
  227. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  228. rect.OffsetRect(2,2);
  229. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  230. ::SetBkColor(lpDis->hDC, RGB(0,0xff,0));
  231. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  232. rect.OffsetRect(2,2);
  233. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  234. ::SetBkColor(lpDis->hDC, RGB(0xff,0xff,0));
  235. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  236. ::SelectObject(lpDis->hDC,hOldPen);
  237. }
  238. }
  239. break;
  240. case WM_SETCOLOR:
  241. crtColor = (COLORREF)wParam;
  242. oldColor = crtColor;
  243. break;
  244. case WM_COMMAND:
  245. ::PostMessage(hwndParent,WM_COLORDLG,NULL,NULL);
  246. break;
  247. case WM_MOUSEMOVE:
  248. {
  249. BOOL bInCtrlArea = FALSE;
  250. point.x = LPARAM_X(lParam);
  251. point.y = LPARAM_Y(lParam);
  252. ::ClientToScreen(hWnd,&point);
  253. if(point.x != oldPoint.x || point.y != oldPoint.y)
  254. {
  255. bMouseMoved = TRUE;
  256. HWND hwndPoint = ::WindowFromPoint(point);
  257. if(hwndPoint)
  258. {
  259. COLORREF crtTMPColor;
  260. CRect rect;
  261. HDC hDC;
  262. hDC = ::GetWindowDC(hwndPoint);
  263. ::GetWindowRect(hwndPoint,&rect);
  264. crtTMPColor = GetPixel(hDC,point.x-rect.left,point.y-rect.top);
  265. ::ReleaseDC(hwndPoint,hDC);
  266. RECT redrawRect;
  267. redrawRect.left = 0;
  268. redrawRect.right = 150;
  269. redrawRect.top = 120;
  270. redrawRect.bottom = 145;
  271. ::GetWindowRect(hWnd,&rect);
  272. if(!rect.PtInRect(point))//不在本窗口
  273. {
  274. crtColor = crtTMPColor;
  275. ::InvalidateRect(hWnd,&redrawRect,TRUE);
  276. }
  277. else
  278. {
  279. rect.bottom = rect.top + 120;
  280. int iCurrentActiveRect;
  281. if(rect.PtInRect(point))///判断鼠标是否在当前窗口的色彩区域
  282. {
  283. ////计算位于哪个方框内////
  284. point.x -= rect.left;
  285. point.y -= rect.top;
  286. if(point.x%10 != 0 && point.y%10 != 0)
  287. iCurrentActiveRect = point.y/10*19 + point.x/10;
  288. else
  289. iCurrentActiveRect = -1;
  290. }
  291. else
  292. {
  293. iCurrentActiveRect = -1;
  294. crtColor = oldColor;
  295. if(hwndColorBtn == hwndPoint)
  296. ::ReleaseCapture();
  297. else
  298. bInCtrlArea = TRUE;
  299. }
  300. if(iCurrentActiveRect != iActiveRect)
  301. {
  302. ///清除原焦点
  303. CRect ActiveRect;
  304. ActiveRect.left = iActiveRect%19*10;
  305. ActiveRect.top = iActiveRect/19*10;
  306. ActiveRect.right = ActiveRect.left + 10;
  307. ActiveRect.bottom = ActiveRect.top + 10;
  308. ::InvalidateRect(hWnd,&ActiveRect,TRUE);
  309. ///新焦点
  310. if(iCurrentActiveRect != -1)
  311. {
  312. crtColor = crtTMPColor;
  313. ActiveRect.left = iCurrentActiveRect%19*10;
  314. ActiveRect.top = iCurrentActiveRect/19*10;
  315. ActiveRect.right = ActiveRect.left + 10;
  316. ActiveRect.bottom = ActiveRect.top + 10;
  317. ::InvalidateRect(hWnd,&ActiveRect,TRUE);
  318. }
  319. ::InvalidateRect(hWnd,&redrawRect,TRUE);
  320. iActiveRect = iCurrentActiveRect;
  321. }
  322. }
  323. }
  324. }
  325. if(bInCtrlArea)
  326. ::PostMessage(hwndParent,WM_SETCURSOR,NULL,MAKELPARAM(1,0));
  327. else
  328. ::PostMessage(hwndParent,WM_SETCURSOR,NULL,NULL);
  329. }
  330. break;
  331. case WM_ACTIVATE:
  332. if(LOWORD(wParam) != WA_INACTIVE)
  333. break;
  334. case WM_LBUTTONUP:
  335. point.x = LPARAM_X(lParam);
  336. point.y = LPARAM_Y(lParam);
  337. if(bMouseMoved || ( point.x == oldPoint.x && point.y == oldPoint.y ))
  338. ::PostMessage(hwndParent,WM_LBUTTONUP,(UINT)crtColor,lParam);
  339. break;
  340. case WM_LBUTTONDOWN:
  341. oldPoint.x = LPARAM_X(lParam);
  342. oldPoint.y = LPARAM_Y(lParam);
  343. bMouseMoved = FALSE;
  344. ::InvalidateRect(hWnd,&crtColorRect,TRUE);
  345. break;
  346. case WM_ERASEBKGND:
  347. {
  348. hDC = (HDC)wParam;
  349. HPEN oldPen = (HPEN)::SelectObject(hDC,_penB);
  350. for(int i=0;i<18;i++)
  351. {
  352. ::MoveToEx(hDC,9+i*10,0,NULL);
  353. ::LineTo(hDC,9+i*10,120);
  354. }
  355. for(i=0;i<12;i++)
  356. {
  357. ::MoveToEx(hDC,0,9+i*10,NULL);
  358. ::LineTo(hDC,190,9+i*10);
  359. }
  360. CRect btmRect(0,120,191,150);
  361. ::FillRect(hDC,btmRect,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
  362. ::Rectangle(hDC,crtColorRect.left-1,crtColorRect.top-1,crtColorRect.right + 1,crtColorRect.bottom + 1);
  363. ::SelectObject(hDC,oldPen);
  364. }
  365. break;
  366. case WM_PAINT:
  367. {
  368. hDC = ::BeginPaint(hWnd,&ps);
  369. CDC dc;
  370. dc.Attach(hDC);
  371. CRect rect;
  372. int x1,y1,i,j,k;
  373. UCHAR R=255,G=255,B=255;
  374. for(i=0;i<12;i++)
  375. {
  376. y1 = i*10;
  377. rect.SetRect(0,y1,9,y1+9);
  378. if(i == 10)
  379. R=G=B=0x17;
  380. dc.FillSolidRect(rect,RGB(R,G,B));
  381. R -= 0x17;
  382. G -= 0x17;
  383. B -= 0x17;
  384. }
  385. R = G = B = 0;
  386. for(k = 0;k < 2;k++)
  387. for(j = 0;j < 18;j++)
  388. for(i = 0;i < 6;i++)
  389. {
  390. x1 = 10+j*10;
  391. y1 = k*60+i*10;
  392. rect.SetRect(x1,y1,x1+9,y1+9);
  393. dc.FillSolidRect(rect,RGB(R,G,B));
  394. if(B == 0xff)
  395. {
  396. B = 0x00;
  397. if(G == 0xff)
  398. {
  399. G = 0x00;
  400. R += 0x33;
  401. }
  402. else
  403. G += 0x33;
  404. }
  405. else
  406. B += 0x33;
  407. }
  408. if(iActiveRect != -1)
  409. {
  410. CRect ActiveRect;
  411. ActiveRect.left = iActiveRect%19*10;
  412. ActiveRect.top = iActiveRect/19*10;
  413. ActiveRect.right = ActiveRect.left + 10;
  414. ActiveRect.bottom = ActiveRect.top + 10;
  415. dc.DrawFocusRect(ActiveRect);
  416. }
  417. rect.SetRect(0,y1+130,60,y1+140);
  418. dc.FillSolidRect(crtColorRect,crtColor);
  419. char strColor[8]="#";
  420. sprintf(strColor+1,"%02X%02X%02X",GetRValue(crtColor),GetGValue(crtColor),GetBValue(crtColor));
  421. HFONT hOldFont = (HFONT)dc.SelectObject(hFont);
  422. dc.SetBkMode(TRANSPARENT);
  423. dc.TextOut(80,127,strColor,7);
  424. dc.SelectObject(hOldFont);
  425. dc.Detach();
  426. ::EndPaint(hWnd,&ps);
  427. }
  428. break;
  429. default:
  430. return(::DefWindowProc(hWnd,nMsg,wParam,lParam));
  431. }
  432. return NULL;
  433. }
  434. BOOL CColorPicker::CreatePaletteWindow()
  435. {
  436. if(!m_bPaletteWndActive)
  437. {
  438. // 创建调色板子窗口
  439. WNDCLASS wndcls;
  440. wndcls.style = CS_HREDRAW | CS_VREDRAW;
  441. wndcls.lpfnWndProc = PaletteWndProc;
  442. wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
  443. wndcls.hInstance = ::AfxGetInstanceHandle();
  444. wndcls.hIcon = NULL;
  445. wndcls.hCursor = NULL;
  446. wndcls.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
  447. wndcls.lpszMenuName = NULL;
  448. wndcls.lpszClassName = "ColorPalette";
  449. if (!::RegisterClass(&wndcls))
  450. AfxThrowResourceException();
  451. HDC hDC = ::GetDC(m_hWnd);
  452. int scrWidth = ::GetDeviceCaps(hDC,HORZRES);
  453. int scrHeight = ::GetDeviceCaps(hDC,VERTRES);
  454. ::ReleaseDC(m_hWnd,hDC);
  455. CRect rect;
  456. GetWindowRect(rect);
  457. rect.top = rect.bottom - 1;
  458. if(rect.left > scrWidth-191)
  459. rect.left = scrWidth-191;
  460. else if(rect.left < 0)
  461. rect.left = 0;
  462. if(rect.top > scrHeight-150)
  463. rect.top = rect.top-150;
  464. rect.bottom = rect.top + 150;
  465. rect.right = rect.left + 191;
  466. if(!(m_hPaletteWnd = ::CreateWindowEx(WS_EX_TOPMOST,"ColorPalette","Palatte",WS_POPUP|WS_BORDER|WS_VISIBLE,rect.left,rect.top,rect.Width(),rect.Height(),m_hWnd,NULL,wndcls.hInstance,NULL)))
  467. return FALSE;
  468. }
  469. ::PostMessage(m_hPaletteWnd,WM_SETCOLOR,(WPARAM)m_CurrentColor,NULL);
  470. ::ShowWindow(m_hPaletteWnd,SW_SHOW);
  471. ::SetCapture(m_hPaletteWnd);
  472. m_bPaletteWndActive = TRUE;
  473. return TRUE;
  474. }
  475. void CColorPicker::DestroyPaletteWindow()
  476. {
  477. ::DestroyWindow(m_hPaletteWnd);
  478. ::UnregisterClass("ColorPalette",::AfxGetInstanceHandle());
  479. m_bPaletteWndActive = FALSE;
  480. m_hPaletteWnd = NULL;
  481. }
  482. BOOL CColorPicker::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  483. {
  484. if(m_bPaletteWndActive && m_hCursorStraw && nHitTest != 1)
  485. ::SetCursor(m_hCursorStraw);
  486. else
  487. ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  488. return TRUE;
  489. }
  490. void CColorPicker::GetColor(CString& strColor)
  491. {
  492. char cColor[8]="#";
  493. sprintf(cColor+1,"%02X%02X%02X",GetRValue(m_CurrentColor),GetGValue(m_CurrentColor),GetBValue(m_CurrentColor));
  494. strColor = cColor;
  495. }
  496. COLORREF CColorPicker::GetColor()
  497. {
  498. return m_CurrentColor;
  499. }
  500. LRESULT CColorPicker::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  501. {
  502. if(message == WM_COLORDLG)
  503. {
  504. DestroyPaletteWindow();
  505. CColorDialog dlg;
  506. dlg.m_cc.Flags |= CC_FULLOPEN;
  507. if(dlg.DoModal() == IDOK)
  508. SetColor(dlg.GetColor());
  509. return TRUE;
  510. }
  511. else
  512. return CButton::DefWindowProc(message, wParam, lParam);
  513. }
  514. void CColorPicker::SetBuddy(HWND hWnd)
  515. {
  516. m_hwndBuddy = hWnd;
  517. }
  518. void CColorPicker::SetColor(COLORREF ref)
  519. {
  520. m_CurrentColor = ref;
  521. ::PostMessage(GetParent()->m_hWnd,WM_COMMAND,MAKELPARAM(::GetWindowLong(m_hWnd,GWL_ID),BN_CLICKED),(LPARAM)m_hWnd);
  522. if(m_hwndBuddy)
  523. {
  524. CString strColor;
  525. GetColor(strColor);
  526. ::SetWindowText(m_hwndBuddy,strColor);
  527. }
  528. Invalidate();
  529. }