CheckBoxExt.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // CheckBoxExt.cpp: implementation of the CCheckBoxExt class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CheckBoxExt.h"
  6. #include "library.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. WNDPROC CCheckBoxExt::m_cWndProc = NULL;
  13. int CCheckBoxExt::pos =0;
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. LRESULT CCheckBoxExt::DefWindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  18. {
  19. CWnd *pWnd = NULL;
  20. CPoint point;
  21. LRESULT lReturn;
  22. TRACKMOUSEEVENT Tme;
  23. pos=FindPos((DWORD)hWnd, g_wndstatelist);
  24. pos++;
  25. pWnd = CWnd::FromHandle( hWnd );
  26. switch( message )
  27. {
  28. case WM_ERASEBKGND:
  29. return 1;
  30. case WM_NCPAINT:
  31. case WM_PAINT:
  32. CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  33. OnPaint( pWnd );
  34. return 1;
  35. case BM_SETSTYLE: // 按钮风格改变
  36. CXPM_SETSTATE(g_wndstatelist.ElementAt (pos), CXPS_DEFAULT, wParam & BS_DEFPUSHBUTTON);
  37. OnPaint( pWnd );
  38. break;
  39. case BM_SETSTATE: // 设置按钮状态
  40. lReturn = (LONG) CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  41. CXPM_SETSTATE(g_wndstatelist.ElementAt (pos), CXPS_PRESSED, wParam);
  42. OnPaint( pWnd );
  43. return lReturn;
  44. case BM_SETCHECK: // 设置选中状态
  45. lReturn = (LONG) CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  46. CXPM_SETSTATE(g_wndstatelist.ElementAt (pos), CXPS_CHECKED, (wParam == BST_CHECKED));
  47. CXPM_SETSTATE(g_wndstatelist.ElementAt (pos), CXPS_INDETERMINATE, (wParam == BST_INDETERMINATE));
  48. OnPaint( pWnd );
  49. return lReturn;
  50. case WM_SETTEXT: // 设置窗口文本
  51. lReturn = (LONG) CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  52. OnPaint( pWnd );
  53. return lReturn;
  54. case WM_MOUSELEAVE: // 鼠标移出
  55. if (g_wndstatelist.ElementAt (pos) & CXPS_HOTLIGHT)
  56. {
  57. g_wndstatelist.ElementAt (pos) &= ~CXPS_HOTLIGHT;
  58. }
  59. if (g_wndstatelist.ElementAt (pos) & CXPS_PRESSED)
  60. {
  61. g_wndstatelist.ElementAt (pos) &= ~CXPS_PRESSED;
  62. }
  63. OnPaint( pWnd );
  64. return 0;
  65. case WM_MOUSEMOVE: // 窗口移动
  66. if (((g_wndstatelist.ElementAt (pos) & CXPS_HOTLIGHT) == 0) && ((wParam & MK_LBUTTON) == 0))
  67. {
  68. g_wndstatelist.ElementAt (pos) |= CXPS_HOTLIGHT;
  69. OnPaint( pWnd );
  70. // 追踪鼠标移出消息一次
  71. Tme.cbSize = sizeof(TRACKMOUSEEVENT);
  72. Tme.dwFlags = TME_LEAVE;
  73. Tme.hwndTrack = hWnd;
  74. _TrackMouseEvent(&Tme);
  75. }
  76. break;
  77. case WM_ENABLE: // 窗口被设置为禁用或可用
  78. CXPM_SETSTATE(g_wndstatelist.ElementAt (pos), CXPS_DISABLED, !wParam);
  79. pWnd->Invalidate ();
  80. return FALSE;
  81. OnPaint( pWnd );
  82. break;
  83. case WM_DESTROY:
  84. {
  85. g_wndstatelist.RemoveAt(pos);
  86. g_wndstatelist.RemoveAt(pos-1);
  87. break;
  88. }
  89. default:
  90. break;
  91. }
  92. return CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  93. }
  94. void CCheckBoxExt::OnPaint( CWnd *pWnd )
  95. {
  96. int i;
  97. RECT Rect, rcText;
  98. MEMDCXP Mdcxp;
  99. HANDLE hHandle;
  100. char szTemp[256];
  101. COLORREF crColor;
  102. DWORD dwStyle;
  103. HDC hDC;
  104. // 获取内存兼容设备场景
  105. Mdcxp.hWnd = pWnd->GetSafeHwnd ();
  106. Mdcxp.bTransfer = FALSE;
  107. Mdcxp.hBitmap = NULL;
  108. g_bDrawBk=1;
  109. GetMemDCXP(&Mdcxp);
  110. hDC = Mdcxp.hMemDC;
  111. // 获取窗口大小
  112. GetWindowRect( pWnd->GetSafeHwnd (), &Rect);
  113. Rect.right -= Rect.left;
  114. Rect.bottom -= Rect.top;
  115. Rect.left = Rect.top = 0;
  116. // 填充背景 g_crDialogbkColor
  117. // hHandle = CreateSolidBrush(g_crDialogbkColor);
  118. // FillRect(hDC,&Rect, (HBRUSH)hHandle);
  119. // DeleteObject(hHandle);
  120. //按显示模式调整位置
  121. dwStyle = GetWindowLong(pWnd->GetSafeHwnd (), GWL_STYLE);
  122. if (Rect.bottom > 13)
  123. {
  124. Rect.top = (Rect.bottom - 13)/2;
  125. Rect.bottom = Rect.top + 13;
  126. }
  127. if (dwStyle & BS_LEFTTEXT)
  128. {
  129. rcText = Rect;
  130. rcText.right -= 17;
  131. Rect.left = Rect.right - 13;
  132. }
  133. else
  134. {
  135. rcText = Rect;
  136. rcText.left += 17;
  137. Rect.left = 0;
  138. Rect.right = 13;
  139. }
  140. // 画最外面的框
  141. FrameRect(Mdcxp.hMemDC, &Rect, (HBRUSH) g_frameedgebrush);
  142. // 画热点框渐变背景
  143. InflateRect(&Rect, -1, -1);
  144. if (!(g_wndstatelist.ElementAt (pos) & CXPS_DISABLED))
  145. {
  146. if (g_wndstatelist.ElementAt (pos) & CXPS_PRESSED)
  147. i = 0;
  148. else if (g_wndstatelist.ElementAt (pos) & CXPS_HOTLIGHT)
  149. i = 1;
  150. else
  151. i = 2;
  152. GradientRectXP(hDC, &Rect, gChecks_crGradientXP[i]);
  153. }
  154. // 画内框
  155. InflateRect(&Rect, -2, -2);
  156. if ((g_wndstatelist.ElementAt (pos) & CXPS_INDETERMINATE) ||
  157. ((g_wndstatelist.ElementAt (pos) & CXPS_HOTLIGHT) && (!(g_wndstatelist.ElementAt (pos) & CXPS_PRESSED))))
  158. {
  159. if (g_wndstatelist.ElementAt (pos) & CXPS_INDETERMINATE)
  160. {
  161. if (g_wndstatelist.ElementAt (pos) & CXPS_DISABLED)
  162. crColor = 0x00BDCBCE;
  163. else if (g_wndstatelist.ElementAt (pos) & CXPS_PRESSED)
  164. crColor = 0x00188A18;
  165. else if (g_wndstatelist.ElementAt (pos) & CXPS_HOTLIGHT)
  166. crColor = 0x0021A221;
  167. else
  168. crColor = 0x0073C373;
  169. }
  170. else if (g_wndstatelist.ElementAt (pos) & CXPS_CHECKED)
  171. crColor = 0x00F7F7F7;
  172. else
  173. crColor = 0x00E7E7E7;
  174. hHandle = (HANDLE) CreateSolidBrush(crColor);
  175. FillRect(hDC, &Rect, (HBRUSH) hHandle);
  176. DeleteObject((HGDIOBJ) hHandle);
  177. }
  178. // 画框内选中标志
  179. if (g_wndstatelist.ElementAt (pos) & CXPS_CHECKED)
  180. {
  181. hHandle = (HANDLE) SelectObject(hDC,CreatePen(PS_SOLID, 1, 0x0021A221));
  182. for (i = 3; i < 10; i++)
  183. {
  184. MoveToEx(hDC, Rect.left+i-3, Rect.top + ((i < 6) ? i - 1 : (9 - i)), NULL);
  185. LineTo(hDC, Rect.left+i-3, Rect.top + ((i < 6) ? i + 2 : (12 - i)));
  186. }
  187. DeleteObject(SelectObject(hDC,(HGDIOBJ) hHandle));
  188. }
  189. // 写文字
  190. if (GetWindowText(pWnd->GetSafeHwnd (), szTemp, sizeof(szTemp)))
  191. {
  192. SetBkColor(hDC, g_crDialogbkColor);
  193. SetTextColor(hDC, GetSysColor((g_wndstatelist.ElementAt (pos) & CXPS_DISABLED) ? COLOR_GRAYTEXT: COLOR_BTNTEXT));
  194. hHandle = (HANDLE) SelectObject(hDC,
  195. (HGDIOBJ) SendMessage(pWnd->GetSafeHwnd (), WM_GETFONT, 0, 0));
  196. rcText.bottom = rcText.top + 1 + DrawText(hDC, szTemp, -1, &rcText,
  197. DT_CALCRECT | DT_SINGLELINE | DT_VCENTER);
  198. DrawText(hDC, szTemp, -1, &rcText, DT_SINGLELINE | DT_VCENTER);
  199. SelectObject(hDC, (HGDIOBJ) hHandle);
  200. }
  201. Mdcxp.bTransfer = TRUE;
  202. ReleaseMemDCXP(&Mdcxp);
  203. }