ColorPicker.cpp
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:20k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "ColorPicker.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. #define LPARAM_X(lp)                        ((int)(short)LOWORD(lp))
  9. #define LPARAM_Y(lp)                        ((int)(short)HIWORD(lp))
  10. #define WM_SETCOLOR WM_USER+1
  11. #define WM_COLORDLG WM_USER+2
  12. #define IDC_COLORDLG_BUTTON 100
  13. CPen _pen3DDKShadow(PS_SOLID,1,::GetSysColor(COLOR_3DDKSHADOW));
  14. CPen _penW(PS_SOLID,1,RGB(0xff,0xff,0xff));
  15. CPen _penB(PS_SOLID,1,RGB(0,0,0));
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CColorPicker
  18. CColorPicker::CColorPicker()
  19. {
  20. //const unsigned char pvANDPlaneC[]={0xff,0xf1,0xff,0xe0,0xff,0xc0,0xff,0x00,0xff,0x81,0xff,0x03,0xfe,0x07,0xfc,0x17,
  21. //0xf8,0x3f,0xf0,0x7f,0xe0,0xff,0xc1,0xff,0x83,0xff,0x87,0xff,0x0f,0xff,0x3f,0xff};
  22. //const unsigned char pvXORPlaneC[]={0x00,0x00,0x00,0x0c,0x00,0x08,0x00,0x10,0x00,0x00,0x00,0x40,0x00,0xe0,0x01,0xc0,
  23. //0x03,0x80,0x07,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x30,0x00,0x40,0x00,0x00,0x00};
  24. int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
  25. int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
  26. m_hCursorStraw = NULL;
  27. //if(cxCursor >=16 && cxCursor < 100 && cyCursor < 100 && cyCursor >= 16)
  28. // {
  29. // int size = cxCursor*cyCursor/8;
  30. // unsigned char *pvANDPlane = new unsigned char[size];
  31. // unsigned char *pvXORPlane = new unsigned char[size];
  32. // if(pvANDPlane && pvXORPlane)
  33. // {
  34. // memset(pvANDPlane,0xff,size);
  35. // memset(pvXORPlane,0x00,size);
  36. // for(int j=0;j<16;j++)
  37. // for(int i=0;i<2;i++)
  38. // {
  39. // *(pvANDPlane+j*cxCursor/8 + i) = *(pvANDPlaneC + j*2 + i);
  40. // *(pvXORPlane+j*cxCursor/8 + i) = *(pvXORPlaneC + j*2 + i);
  41. // }
  42. // m_hCursorStraw = ::CreateCursor(::AfxGetInstanceHandle(),0,15,cxCursor,cyCursor,pvANDPlane,pvXORPlane);
  43. // delete pvANDPlane;
  44. // delete pvXORPlane;
  45. // }
  46. // }
  47. m_CurrentColor = COLORREF(::GetSysColor(COLOR_3DFACE));
  48. m_hwndBuddy = NULL;
  49. m_hPaletteWnd = NULL;
  50. m_bPaletteWndActive = FALSE;
  51. m_bMouseDown=FALSE;
  52. }
  53. CColorPicker::~CColorPicker()
  54. {
  55. ::DestroyCursor(m_hCursorStraw);
  56. }
  57. BEGIN_MESSAGE_MAP(CColorPicker, CButton)
  58. //{{AFX_MSG_MAP(CColorPicker)
  59. ON_WM_LBUTTONDOWN()
  60. ON_WM_LBUTTONUP()
  61. ON_WM_SETCURSOR()
  62. ON_WM_PAINT()
  63. ON_WM_MOUSEMOVE()
  64. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  65. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  66. ON_WM_LBUTTONDBLCLK()
  67. //}}AFX_MSG_MAP
  68. END_MESSAGE_MAP()
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CColorPicker message handlers
  71. void CColorPicker::OnLButtonDown(UINT nFlags, CPoint point) 
  72. {
  73. // CButton::OnLButtonDown(nFlags, point);
  74. m_bMouseDown=TRUE;
  75.     if(GetSelRect().PtInRect(point))//如果点中的是右边箭头部分
  76. {
  77.      ::ClientToScreen(this->m_hWnd,&point);
  78.  if(CreatePaletteWindow())
  79.  {
  80. nFlags = (UINT)m_CurrentColor;
  81. ::PostMessage(m_hPaletteWnd,WM_LBUTTONDOWN,nFlags,MAKELPARAM(point.x,point.y));
  82.  }
  83.      HWND hwndParent = GetParent()->m_hWnd;
  84.     if(hwndParent)
  85.  {
  86. POINT p1,p2;
  87. RECT rect;
  88. ::GetWindowRect(m_hWnd,&rect);
  89. p1.x = rect.left;
  90. p1.y = rect.top;
  91. p2.x = rect.right;
  92. p2.y = rect.bottom;
  93. ::ScreenToClient(hwndParent,&p1);
  94. ::ScreenToClient(hwndParent,&p2);
  95. rect.left = p1.x;
  96. rect.top = p1.y;
  97. rect.right = p2.x;
  98. rect.bottom = p2.y;
  99. ::InvalidateRect(hwndParent,&rect,TRUE);
  100.  }
  101. }
  102. else
  103. {
  104.          
  105. //如果点中的是左边部分。。。
  106. CPaintDC dc(this); // device context for painting 
  107. dc.FillSolidRect(m_rectFill,m_CurrentColor);
  108. }
  109. }
  110. void CColorPicker::OnLButtonUp(UINT nFlags, CPoint point) 
  111. {
  112.     if(NULL!=m_hPaletteWnd)
  113. DestroyPaletteWindow();
  114. m_bMouseDown=FALSE;
  115. if((COLORREF)nFlags != m_CurrentColor)
  116. SetColor((COLORREF)nFlags);
  117. else
  118. Invalidate();
  119. }
  120. LONG FAR PASCAL CColorPicker::PaletteWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  121. {
  122. HDC hDC;
  123. PAINTSTRUCT ps;
  124. POINT point;
  125. CRect crtColorRect(5,87,50,103);
  126. static POINT oldPoint;
  127. static BOOL bMouseMoved = FALSE;
  128. static HWND hwndParent;
  129. static HWND hwndColorBtn;
  130. static HFONT hFont = NULL;
  131. static COLORREF crtColor,oldColor;
  132. static int iActiveRect = -1;
  133. HMODULE hModule;
  134.     typedef BOOL (WINAPI* SETLAYEREDWND)( HWND, COLORREF, BYTE, DWORD);
  135. SETLAYEREDWND SetLayeredWindowPtr = NULL;
  136. LONG lStyle;
  137. switch(nMsg)
  138. {
  139. case WM_CREATE:
  140. {
  141. hwndParent = ((LPCREATESTRUCT)lParam)->hwndParent;
  142. crtColor = ::GetSysColor(COLOR_3DFACE);
  143. LOGFONT logFont;
  144. ZeroMemory((void*)&logFont,sizeof(logFont));
  145. strcpy(logFont.lfFaceName,"宋体");
  146. logFont.lfHeight = -12;
  147. logFont.lfWeight = 400;
  148. logFont.lfCharSet = GB2312_CHARSET;
  149. logFont.lfOutPrecision = 3;
  150. logFont.lfClipPrecision = 2; 
  151. logFont.lfQuality = 1;
  152. logFont.lfPitchAndFamily = 2;
  153. hFont = ::CreateFontIndirect(&logFont);
  154. ////色彩对话框按钮
  155. hwndColorBtn = ::CreateWindow("BUTTON","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,135,90,16,16,hWnd,HMENU(IDC_COLORDLG_BUTTON),::AfxGetInstanceHandle(),NULL);
  156. if(hwndColorBtn)
  157. ::ShowWindow(hwndColorBtn,SW_SHOW);
  158. }
  159. //获取User32.DLL的句柄
  160. hModule =GetModuleHandle("User32.DLL");    
  161. if(hModule == NULL)
  162. {
  163. //如果没有得到就退出
  164.     break;
  165. }
  166. //定义函数指针
  167. //从User32.DLL中获得函数指针
  168. SetLayeredWindowPtr = (SETLAYEREDWND)GetProcAddress(hModule, "SetLayeredWindowAttributes");
  169. if(SetLayeredWindowPtr)
  170. {
  171. //获取原有窗口风格并加入WS_SYSMENU风格,因为只有此风格的窗口才能设置为透明
  172. lStyle = GetWindowLong(hwndColorBtn, GWL_EXSTYLE) | WS_SYSMENU;
  173. //设置窗口风格
  174. SetWindowLong( hWnd, GWL_EXSTYLE, lStyle);    
  175. //设置窗口透明
  176. SetLayeredWindowPtr( hWnd, 0, 100, 2);
  177. FreeLibrary(hModule);
  178. }    
  179. break;
  180. case WM_DESTROY:
  181. ::DeleteObject(hFont);
  182. break;
  183. case WM_DRAWITEM:
  184. {
  185. LPDRAWITEMSTRUCT lpDis = (LPDRAWITEMSTRUCT)lParam;
  186. if(lpDis->CtlID == IDC_COLORDLG_BUTTON)
  187. {
  188. CRect rect(3,3,8,8);
  189. ::FillRect(lpDis->hDC,&lpDis->rcItem,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
  190. HPEN hOldPen = (HPEN)::SelectObject(lpDis->hDC,_penW);
  191. if(lpDis->itemState == 17)
  192. {
  193. ::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
  194. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
  195. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
  196. ::SelectObject(lpDis->hDC,_penB);
  197. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
  198. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
  199. rect.OffsetRect(1,1);
  200. }
  201. else
  202. {
  203. ::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
  204. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
  205. ::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
  206. ::SelectObject(lpDis->hDC,_penB);
  207. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
  208. ::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
  209. }
  210. ::SelectObject(lpDis->hDC,_pen3DDKShadow);
  211. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  212. ::SetBkColor(lpDis->hDC, RGB(0xff,0,0));
  213. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  214. rect.OffsetRect(2,2);
  215. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  216. ::SetBkColor(lpDis->hDC, RGB(0,0xff,0));
  217. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  218. rect.OffsetRect(2,2);
  219. ::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
  220. ::SetBkColor(lpDis->hDC, RGB(0xff,0xff,0));
  221. ::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  222. ::SelectObject(lpDis->hDC,hOldPen);
  223. }
  224. }
  225. break;
  226. case WM_SETCOLOR:
  227. crtColor = (COLORREF)wParam;
  228. oldColor = crtColor;
  229. break;
  230. case WM_COMMAND:
  231. ::PostMessage(hwndParent,WM_COLORDLG,NULL,NULL);
  232. break;
  233. case WM_MOUSEMOVE:
  234. {
  235. BOOL bInCtrlArea = FALSE;
  236. point.x = LPARAM_X(lParam);
  237. point.y = LPARAM_Y(lParam);
  238. ::ClientToScreen(hWnd,&point);
  239. if(point.x != oldPoint.x || point.y != oldPoint.y)
  240. {
  241. bMouseMoved = TRUE;
  242. HWND hwndPoint = ::WindowFromPoint(point);
  243. if(hwndPoint)
  244. {
  245. COLORREF crtTMPColor;
  246. CRect rect;
  247. HDC hDC;
  248. hDC = ::GetWindowDC(hwndPoint);
  249. ::GetWindowRect(hwndPoint,&rect);
  250. int i=0,j=0;
  251. i=(point.x-rect.left)/20;
  252. j=(point.y-rect.top)/10;
  253. crtTMPColor = GetPixel(hDC,i*20 + 5,j*10+5);
  254. ::ReleaseDC(hwndPoint,hDC);
  255. RECT redrawRect;
  256. redrawRect.left = 0;
  257. redrawRect.right = 160;
  258. redrawRect.top = 80;
  259. redrawRect.bottom = 110;
  260. ::GetWindowRect(hWnd,&rect);
  261. rect.bottom = rect.top + 80;
  262. int iCurrentActiveRect;
  263. if(rect.PtInRect(point))///判断鼠标是否在当前窗口的色彩区域
  264. {
  265. ////计算位于哪个方框内////
  266. point.x -= rect.left;
  267. point.y -= rect.top;
  268. if(point.x%20 != 0 && point.y%10 != 0)
  269. iCurrentActiveRect = point.y/10*8 + point.x/20;
  270. else
  271. iCurrentActiveRect = -1;
  272. }
  273. else
  274. {
  275. iCurrentActiveRect = -1;
  276. crtColor = oldColor;
  277. if(hwndColorBtn == hwndPoint)
  278. ::ReleaseCapture();
  279. else
  280. bInCtrlArea = TRUE;
  281. }
  282. if(iCurrentActiveRect != iActiveRect)
  283. {
  284. ///清除原焦点
  285. CRect ActiveRect;
  286. ActiveRect.left = iActiveRect%8*20;
  287. ActiveRect.top = iActiveRect/8*10;
  288. ActiveRect.right = ActiveRect.left + 20;
  289. ActiveRect.bottom = ActiveRect.top + 10;
  290. ::InvalidateRect(hWnd,&ActiveRect,TRUE);
  291. ///新焦点
  292. if(iCurrentActiveRect != -1)
  293. {
  294. crtColor = crtTMPColor;
  295. ActiveRect.left = iCurrentActiveRect%8*20;
  296. ActiveRect.top = iCurrentActiveRect/8*10;
  297. ActiveRect.right = ActiveRect.left + 20;
  298. ActiveRect.bottom = ActiveRect.top + 10;
  299. ::InvalidateRect(hWnd,&ActiveRect,TRUE);
  300. }
  301. ::InvalidateRect(hWnd,&redrawRect,TRUE);
  302. iActiveRect = iCurrentActiveRect;
  303. }
  304. }
  305. }
  306. if(bInCtrlArea)
  307. ::PostMessage(hwndParent,WM_SETCURSOR,NULL,MAKELPARAM(1,0));
  308. else
  309. ::PostMessage(hwndParent,WM_SETCURSOR,NULL,NULL);
  310. }
  311. break;
  312. case WM_ACTIVATE:
  313. if(LOWORD(wParam) != WA_INACTIVE)
  314. break;
  315. case WM_LBUTTONUP:
  316. point.x = LPARAM_X(lParam);
  317. point.y = LPARAM_Y(lParam);
  318. if(bMouseMoved || ( point.x == oldPoint.x && point.y == oldPoint.y ))
  319. ::PostMessage(hwndParent,WM_LBUTTONUP,(UINT)crtColor,lParam);
  320. break;
  321. case WM_LBUTTONDOWN:
  322. oldPoint.x = LPARAM_X(lParam);
  323. oldPoint.y = LPARAM_Y(lParam);
  324. bMouseMoved = FALSE;
  325. ::InvalidateRect(hWnd,&crtColorRect,TRUE);
  326. break;
  327. case WM_ERASEBKGND:
  328. {
  329. hDC = (HDC)wParam;
  330. HPEN oldPen = (HPEN)::SelectObject(hDC,_penB);
  331. for(int i=0;i<=8;i++)
  332. {
  333. ::MoveToEx(hDC,i*20,0,NULL);
  334. ::LineTo(hDC,i*20,80);
  335. }
  336. for(i=0;i<=8;i++)
  337. {
  338. ::MoveToEx(hDC,0,i*10,NULL);
  339. ::LineTo(hDC,160,i*10);
  340. }
  341. CRect btmRect(0,80,160,110);
  342. ::FillRect(hDC,btmRect,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
  343. ::Rectangle(hDC,crtColorRect.left-1,crtColorRect.top-1,crtColorRect.right + 1,crtColorRect.bottom + 1);
  344. ::SelectObject(hDC,oldPen);
  345. }
  346. break;
  347. case WM_PAINT:
  348. {
  349. hDC = ::BeginPaint(hWnd,&ps);
  350. CDC dc;
  351. dc.Attach(hDC);
  352. CRect rect;
  353. int x1,y1,i,j,k;
  354. UCHAR R=0,G=0,B=0;
  355. //xgl 2004.3.11
  356. //初始颜色的配置:分R,G,B三块,4*4为基元,先行后列,B 增加;基元内:R按行增加,G按列增加
  357. for(j = 0;j < 8;j++)
  358. for(i = 0;i < 8;i++)
  359. {
  360. x1 = j*20;
  361. y1 = i*10;
  362. //85=256/3;
  363. R=85*(i%4);
  364. G=85*(j%4);
  365. B=((i/4)*2+j/4)*85;
  366. rect.SetRect(x1,y1,x1+20,y1+10);
  367. dc.FillSolidRect(rect,RGB(0,200,200));
  368. rect.SetRect(x1+2,y1+1,x1+18,y1+9);
  369. dc.FillSolidRect(rect,RGB(10,10,10));
  370. rect.SetRect(x1+3,y1+2,x1+17,y1+8);
  371. dc.FillSolidRect(rect,RGB(R,G,B));
  372. }
  373. if(iActiveRect != -1)
  374. {
  375. CRect ActiveRect;
  376. ActiveRect.left = iActiveRect%8*20;
  377. ActiveRect.top = iActiveRect/8*10;
  378. ActiveRect.right = ActiveRect.left + 20;
  379. ActiveRect.bottom = ActiveRect.top + 10;
  380. dc.DrawFocusRect(ActiveRect);
  381. }
  382. // for(i=1;i<8;i++)
  383. // {
  384. // dc.MoveTo(i*20,0);
  385. // dc.LineTo(i*20,80);
  386. // dc.MoveTo(0,i*10);
  387. // dc.LineTo(160,i*10);
  388. // }
  389. rect.SetRect(0,81,160,82);
  390. dc.FillSolidRect(rect,RGB(0,0,0));
  391. rect.SetRect(0,82,160,110);
  392. dc.FillSolidRect(rect,RGB(12,180,180));
  393. dc.FillSolidRect(crtColorRect.left-2,crtColorRect.top-2,crtColorRect.Width()+4,crtColorRect.Height()+4,RGB(0,0,0));
  394. dc.FillSolidRect(crtColorRect,crtColor);
  395. char strColor[8]="#";
  396. sprintf(strColor+1,"%02X%02X%02X",GetRValue(crtColor),GetGValue(crtColor),GetBValue(crtColor));
  397. HFONT hOldFont = (HFONT)dc.SelectObject(hFont);
  398. dc.SetBkMode(TRANSPARENT);
  399. dc.TextOut(80,90,strColor,7);
  400. dc.SelectObject(hOldFont);
  401. dc.Detach();
  402. ::EndPaint(hWnd,&ps);
  403. }
  404. break;
  405. default:
  406. return(::DefWindowProc(hWnd,nMsg,wParam,lParam));
  407. }
  408. return NULL;
  409. }
  410. BOOL CColorPicker::CreatePaletteWindow()
  411. {
  412. if(!m_bPaletteWndActive)
  413. {
  414. // 创建调色板子窗口
  415. WNDCLASS wndcls;
  416. wndcls.style = CS_HREDRAW | CS_VREDRAW;
  417. wndcls.lpfnWndProc = PaletteWndProc;
  418. wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
  419. wndcls.hInstance = ::AfxGetInstanceHandle();
  420. wndcls.hIcon = NULL;
  421. wndcls.hCursor = NULL;
  422. wndcls.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
  423. wndcls.lpszMenuName = NULL;
  424. wndcls.lpszClassName = "ColorPalette";
  425. if (!::RegisterClass(&wndcls))
  426. AfxThrowResourceException();
  427. HDC hDC = ::GetDC(m_hWnd);
  428. int scrWidth = ::GetDeviceCaps(hDC,HORZRES);
  429. int scrHeight = ::GetDeviceCaps(hDC,VERTRES);
  430. ::ReleaseDC(m_hWnd,hDC);
  431. CRect rect;
  432. GetWindowRect(rect);
  433. rect.top = rect.bottom - 1;
  434. if(rect.left > scrWidth-160)
  435. rect.left = scrWidth-160;
  436. else if(rect.left < 0)
  437. rect.left = 0;
  438. if(rect.top > scrHeight-110)
  439. rect.top = rect.top-110;
  440. rect.bottom = rect.top + 110;
  441. rect.right = rect.left + 160;
  442. 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)))
  443. return FALSE;
  444. }
  445. ::PostMessage(m_hPaletteWnd,WM_SETCOLOR,(WPARAM)m_CurrentColor,NULL);
  446. ::ShowWindow(m_hPaletteWnd,SW_SHOW);
  447. ::SetCapture(m_hPaletteWnd);
  448. m_bPaletteWndActive = TRUE;
  449. return TRUE;
  450. }
  451. void CColorPicker::DestroyPaletteWindow()
  452. {
  453. ::DestroyWindow(m_hPaletteWnd);
  454. ::UnregisterClass("ColorPalette",::AfxGetInstanceHandle());
  455. m_bPaletteWndActive = FALSE;
  456. m_hPaletteWnd = NULL;
  457. }
  458. BOOL CColorPicker::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  459. {
  460. if(m_bPaletteWndActive && m_hCursorStraw && nHitTest != 1)
  461. ::SetCursor(m_hCursorStraw);
  462. else
  463. ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  464. return TRUE;
  465. }
  466. void CColorPicker::GetColor(CString& strColor)
  467. {
  468. char cColor[8]="#";
  469. sprintf(cColor+1,"%02X%02X%02X",GetRValue(m_CurrentColor),GetGValue(m_CurrentColor),GetBValue(m_CurrentColor));
  470. strColor = cColor;
  471. }
  472. COLORREF CColorPicker::GetColor()
  473. {
  474. return m_CurrentColor;
  475. }
  476. LRESULT CColorPicker::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  477. {
  478. if(message == WM_COLORDLG)
  479. {
  480. DestroyPaletteWindow();
  481. CColorDialog dlg;
  482. dlg.m_cc.Flags |= CC_FULLOPEN;
  483. if(dlg.DoModal() == IDOK)
  484. SetColor(dlg.GetColor());
  485. return TRUE;
  486. }
  487. else
  488. return CButton::DefWindowProc(message, wParam, lParam);
  489. }
  490. void CColorPicker::SetBuddy(HWND hWnd)
  491. {
  492. m_hwndBuddy = hWnd;
  493. }
  494. void CColorPicker::SetColor(COLORREF ref)
  495. {
  496. m_CurrentColor = ref;
  497. ::PostMessage(GetParent()->m_hWnd,WM_COMMAND,MAKELPARAM(::GetWindowLong(m_hWnd,GWL_ID),BN_CLICKED),(LPARAM)m_hWnd);
  498. if(m_hwndBuddy)
  499. {
  500. CString strColor;
  501. GetColor(strColor);
  502. ::SetWindowText(m_hwndBuddy,strColor);
  503. }
  504. Invalidate();
  505. }
  506. void CColorPicker::OnPaint() 
  507. {
  508. CPaintDC dc(this); // device context for painting
  509. // TODO: Add your message handler code here
  510.   //xiagl@fnst 2004.3.10(add_start)
  511.     //Draw the left half of ColorPicker
  512. dc.FillSolidRect(m_rectFill,RGB(((int)GetRValue(m_CurrentColor)+128)%256,((int)GetGValue(m_CurrentColor)+128)%256,((int)GetBValue(m_CurrentColor)+128)%256));
  513.     CPen pen1,*m_poldPen;
  514. LOGBRUSH m_logbr1;
  515. m_logbr1.lbStyle=BS_SOLID;
  516. m_logbr1.lbColor=m_CurrentColor;
  517. pen1.CreatePen(PS_GEOMETRIC, m_rectFill.Height()/8, &m_logbr1);  
  518. m_poldPen=dc.SelectObject(&pen1);
  519.     dc.MoveTo(m_rectFill.left+m_rectFill.Width()/8,m_rectFill.top+m_rectFill.Height()*7/8);
  520. dc.LineTo(m_rectFill.Width()/2,m_rectFill.top+m_rectFill.Height()/8);
  521. dc.LineTo(m_rectFill.left+m_rectFill.Width()*7/8,m_rectFill.top+m_rectFill.Height()*7/8);
  522. dc.LineTo(m_rectFill.left+m_rectFill.Width()*7/8-2,m_rectFill.top+m_rectFill.Height()*7/8-2);
  523. dc.MoveTo(m_rectFill.left+m_rectFill.Width()*5/16,m_rectFill.top+m_rectFill.Height()/2);
  524. dc.LineTo(m_rectFill.left+m_rectFill.Width()*11/16,m_rectFill.top+m_rectFill.Height()/2);
  525.     dc.SelectObject(m_poldPen);
  526. //Draw the right half of ColorPicker
  527. CPoint points[3];
  528. points[0].x=m_rectSel.left+m_rectSel.Width()*1.0/6;
  529. points[0].y=m_rectSel.Height()*0.5;
  530. points[1].x=m_rectSel.left+m_rectSel.Width()*3.0/6;
  531. points[1].y=m_rectSel.Height()*0.8;
  532. points[2].x=m_rectSel.left+m_rectSel.Width()*5.0/6+1;
  533. points[2].y=m_rectSel.Height()*0.5;
  534. CRgn rgn; 
  535. rgn.CreatePolygonRgn(points,3,1);   
  536. CBrush brush,*p_oldbr;
  537. brush.CreateSolidBrush(RGB(0,0,0));
  538. p_oldbr=dc.SelectObject(&brush);
  539. dc.FillRgn(&rgn,&brush);
  540. dc.SelectObject(p_oldbr); 
  541.   //xiagl@fnst 2004.3.10(add_start)
  542. // Do not call CButton::OnPaint() for painting messages
  543. }
  544. void  CColorPicker::SetFillRect(CRect rect)
  545. {
  546. this->m_rectFill.left=rect.left;
  547. this->m_rectFill.right=rect.right;
  548. this->m_rectFill.top=rect.top;
  549. this->m_rectFill.bottom=rect.bottom;
  550. }
  551. void  CColorPicker::SetFillRect(int left,int top,int right,int bottom)
  552. {
  553.   this->m_rectFill.left=left;
  554.   this->m_rectFill.right=right;
  555.   this->m_rectFill.top=top;
  556.   this->m_rectFill.bottom=bottom;
  557. }
  558. CRect CColorPicker::GetFillRect()
  559. {
  560. return this->m_rectFill;
  561. }
  562. void  CColorPicker::SetSelRect(CRect rect)
  563. {
  564. this->m_rectSel.left=rect.left;
  565. this->m_rectSel.right=rect.right;
  566. this->m_rectSel.top=rect.top;
  567. this->m_rectSel.bottom=rect.bottom;
  568. }
  569. void  CColorPicker::SetSelRect(int left,int top,int right,int bottom)
  570. {
  571. this->m_rectSel.left=left;
  572. this->m_rectSel.right=right;
  573. this->m_rectSel.top=top;
  574. this->m_rectSel.bottom=bottom; 
  575. }
  576. CRect CColorPicker::GetSelRect()
  577. {
  578. return this->m_rectSel;
  579. }
  580. void CColorPicker::OnMouseMove(UINT nFlags, CPoint point) 
  581. {
  582. // TODO: Add your message handler code here and/or call default
  583. TRACKMOUSEEVENT tme;
  584. tme.cbSize = sizeof(tme);
  585. tme.hwndTrack = m_hWnd;
  586. tme.dwFlags = TME_LEAVE | TME_HOVER;
  587. tme.dwHoverTime = 1;
  588.     _TrackMouseEvent(&tme);
  589. CClientDC dc(this);
  590. CPen pen1,pen2,*m_poldPen;
  591.  LOGBRUSH m_logbr1,m_logbr2;
  592.  m_logbr1.lbStyle=BS_SOLID;
  593.  m_logbr1.lbColor=RGB(100,100,100);
  594.  m_logbr2.lbStyle=BS_SOLID;
  595.  m_logbr2.lbColor=RGB(190,190,190);
  596.      
  597.  pen1.CreatePen(PS_GEOMETRIC, 2, &m_logbr1);  
  598.  pen2.CreatePen(PS_GEOMETRIC, 2, &m_logbr2);  
  599.      m_poldPen=dc.SelectObject(&pen1);
  600.     if(m_bMouseDown=TRUE)
  601. {
  602.         dc.SelectObject(&pen1);
  603. dc.MoveTo(m_rectSel.left,0);
  604. dc.LineTo(m_rectSel.left,m_rectSel.bottom);
  605. dc.LineTo(0,m_rectSel.bottom);
  606. dc.SelectObject(&pen2);
  607. dc.LineTo(0,0);
  608. dc.LineTo(m_rectSel.left,0);
  609. dc.MoveTo(m_rectSel.right,0);
  610. dc.LineTo(m_rectSel.right,m_rectSel.Height());
  611. dc.LineTo(m_rectSel.left,m_rectSel.Height());
  612. dc.LineTo(m_rectSel.left,0);
  613. }
  614. else
  615. {
  616. dc.SelectObject(&pen2);
  617. dc.MoveTo(m_rectSel.left,0);
  618. dc.LineTo(m_rectSel.left,m_rectSel.bottom);
  619. dc.LineTo(0,m_rectSel.bottom);
  620. dc.SelectObject(&pen1);
  621. dc.LineTo(0,0);
  622. dc.LineTo(m_rectSel.left,0);
  623. dc.MoveTo(m_rectSel.Width(),0);
  624. dc.LineTo(m_rectSel.Width(),m_rectSel.Height());
  625. dc.LineTo(m_rectSel.left,m_rectSel.Height());
  626. dc.LineTo(m_rectSel.left,0);
  627. }
  628. dc.SelectObject(m_poldPen);
  629. CButton::OnMouseMove(nFlags, point);
  630. }
  631. LRESULT CColorPicker::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  632. {
  633. InvalidateRect(NULL, FALSE);
  634. return 0;
  635. }
  636. LRESULT CColorPicker::OnMouseHover(WPARAM wParam, LPARAM lParam)
  637. {
  638. return 0;
  639. }
  640. void CColorPicker::OnLButtonDblClk(UINT nFlags, CPoint point) 
  641. {
  642. // TODO: Add your message handler code here and/or call default
  643. //CButton::OnLButtonDblClk(nFlags, point);
  644. }