PinButton.cpp
上传用户:wujian85
上传日期:2010-04-08
资源大小:227k
文件大小:6k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // PinButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "printformeditor.h"
  5. #include "PinButton.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CPinButton
  13. CPinButton::CPinButton()
  14. {
  15. pBmp = pCheckBmp = pCheckBmpPressed = NULL;
  16. bCheckMode = false;
  17. bCheckStatus = false;
  18. bHighlight = bLBtnDown = false;
  19. bCloseLock = false;
  20. }
  21. CPinButton::~CPinButton()
  22. {
  23. if (pBmp) delete pBmp;
  24. if (pCheckBmp) delete pCheckBmp;
  25. if (pCheckBmpPressed) delete pCheckBmpPressed;
  26. }
  27. BEGIN_MESSAGE_MAP(CPinButton, CButton)
  28. //{{AFX_MSG_MAP(CPinButton)
  29. ON_WM_CREATE()
  30. ON_WM_TIMER()
  31. ON_WM_MOUSEMOVE()
  32. ON_WM_LBUTTONDOWN()
  33. ON_WM_LBUTTONUP()
  34. ON_WM_KILLFOCUS()
  35. ON_WM_SETFOCUS()
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CPinButton message handlers
  40. void CPinButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  41. {
  42. CDC * pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  43. CRect rect(lpDrawItemStruct->rcItem);
  44. int iObk = pDC->SetBkMode(TRANSPARENT);
  45. UINT state = lpDrawItemStruct->itemState;
  46. int iYOffset = 0, iXOffset = 0;
  47. rect.top  += iYOffset;
  48. rect.left += iXOffset;
  49. CRect bdrRect(rect);
  50. int ix1 = 0, iy1 = 0;
  51. if (bHighlight)
  52. {
  53. if (state & ODS_SELECTED)
  54. {
  55. ix1 = 1;
  56. iy1 = 1;
  57. }
  58. }
  59. CBitmap * pBitmap = pBmp;
  60. if (bCheckMode && bCheckStatus) 
  61. {
  62. if (bHighlight) pBitmap = pCheckBmpPressed;
  63. else pBitmap = pCheckBmp;
  64. }
  65. if (pBitmap)
  66. {
  67. CDC tempDC;
  68. tempDC.CreateCompatibleDC(pDC);
  69. tempDC.SelectObject(pBitmap);
  70. int y = rect.top + 1;
  71. y = (rect.Height() - rcImage.Height()) / 2 + rect.top;
  72. int x = (rect.Width() - rcImage.Width()) / 2 + rect.left;
  73. pDC->BitBlt(x + ix1, y + iy1, rcImage.Width(), rcImage.Height(), &tempDC, 0, 0, SRCCOPY );
  74. rect.top += (rcImage.Height() + 2);
  75. }
  76. if (bCheckMode && bCheckStatus)
  77. pDC->Draw3dRect(bdrRect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
  78. else if (bHighlight)
  79. {
  80. if (state & ODS_SELECTED)
  81. pDC->Draw3dRect(bdrRect,GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
  82. else
  83. pDC->Draw3dRect(bdrRect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
  84. }
  85. }
  86. int CPinButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  87. {
  88. if (CButton::OnCreate(lpCreateStruct) == -1)
  89. return -1;
  90. return 0;
  91. }
  92. void CPinButton::OnTimer(UINT nIDEvent) 
  93. {
  94. static bool pPainted = false;
  95. POINT pt;
  96. GetCursorPos(&pt);
  97. CRect rect;
  98. GetWindowRect (rect);
  99. if (bLBtnDown)
  100. {
  101. KillTimer (1);
  102. if (pPainted) InvalidateRect (NULL);
  103. pPainted = FALSE;
  104. return;
  105. }
  106. if (!rect.PtInRect (pt))
  107. {
  108. bHighlight = false;
  109. KillTimer (1);
  110. if (pPainted)
  111. InvalidateRect(NULL);
  112. pPainted = false;
  113. return;
  114. }
  115. else
  116. {
  117. bHighlight = true;
  118. if (!pPainted)
  119. {
  120. pPainted = true;
  121. InvalidateRect(NULL);
  122. }
  123. }
  124. CButton::OnTimer(nIDEvent);
  125. }
  126. bool CPinButton::SetBitmap(int resId, COLORREF * rmp, const bool bIsCheck)
  127. {
  128. BITMAP bm;
  129. if (bIsCheck)
  130. {
  131. if (!pCheckBmp) pCheckBmp = new CBitmap;
  132. ASSERT(pCheckBmp);
  133. if (rmp)
  134. {
  135. COLORMAP cmMap = { *rmp, GetSysColor(COLOR_3DFACE) };
  136. if (pCheckBmp->LoadMappedBitmap(resId, 0, &cmMap, 1))
  137. {
  138. pCheckBmp->GetBitmap(&bm);
  139. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  140. return true;
  141. }
  142. }
  143. else
  144. {
  145. if (pCheckBmp->LoadBitmap(resId))
  146. {
  147. pCheckBmp->GetBitmap(&bm);
  148. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  149. return true;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. if (!pBmp) pBmp = new CBitmap;
  156. ASSERT(pBmp);
  157. if (rmp)
  158. {
  159. COLORMAP cmMap = { *rmp, GetSysColor(COLOR_3DFACE) };
  160. if (pBmp->LoadMappedBitmap(resId, 0, &cmMap, 1))
  161. {
  162. pBmp->GetBitmap(&bm);
  163. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  164. return true;
  165. }
  166. }
  167. else
  168. {
  169. if (pBmp->LoadBitmap(resId))
  170. {
  171. pBmp->GetBitmap(&bm);
  172. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  173. return true;
  174. }
  175. }
  176. }
  177. return false;
  178. }
  179. bool CPinButton::SetBitmapCheckPress(int resId, COLORREF * rmp)
  180. {
  181. BITMAP bm;
  182. if (!pCheckBmpPressed) pCheckBmpPressed = new CBitmap;
  183. ASSERT(pCheckBmpPressed);
  184. if (rmp)
  185. {
  186. COLORMAP cmMap = { *rmp, GetSysColor(COLOR_3DFACE) };
  187. if (pCheckBmpPressed->LoadMappedBitmap(resId, 0, &cmMap, 1))
  188. {
  189. pCheckBmpPressed->GetBitmap(&bm);
  190. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  191. return true;
  192. }
  193. }
  194. else
  195. {
  196. if (pCheckBmpPressed->LoadBitmap(resId))
  197. {
  198. pCheckBmpPressed->GetBitmap(&bm);
  199. rcImage.SetRect(0,0,bm.bmWidth, bm.bmHeight);
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. void CPinButton::EnableCheckMode(const bool bCheck)
  206. {
  207. bCheckMode = bCheck;
  208. }
  209. bool CPinButton::IsDlgButtonChecked()
  210. {
  211. return bCheckStatus;
  212. }
  213. void CPinButton::OnMouseMove(UINT nFlags, CPoint point) 
  214. {
  215. SetTimer(1,10,NULL);
  216. CButton::OnMouseMove(nFlags, point);
  217. }
  218. void CPinButton::OnLButtonDown(UINT nFlags, CPoint point) 
  219. {
  220. bLBtnDown = true;
  221. CButton::OnLButtonDown(nFlags, point);
  222. }
  223. void CPinButton::OnLButtonUp(UINT nFlags, CPoint point) 
  224. {
  225. bLBtnDown = false;
  226. bool bThis = false;
  227. CPoint rpt(point);
  228. ClientToScreen(&rpt);
  229. CWnd * pWnd = WindowFromPoint(rpt);
  230. if (pWnd && pWnd->GetSafeHwnd() == GetSafeHwnd()) bThis = true;
  231. if (bCheckMode && bThis) 
  232. {
  233. bCheckStatus = !bCheckStatus;
  234. InvalidateRect(NULL);
  235. }
  236. if (bHighlight)
  237. {
  238. bHighlight = false;
  239. InvalidateRect(NULL);
  240. }
  241. // if (bCheckMode) CButton::OnLButtonUp(nFlags, point);
  242. // else 
  243. if (!bCheckMode && bThis)
  244. {
  245. if (!bCheckMode) bCloseLock = true;
  246. GetParent()->SendMessage(WM_COMMAND, MAKELONG(GetDlgCtrlID(), BN_CLICKED), (LPARAM) GetSafeHwnd());
  247. if (!bCheckMode) bCloseLock = false;
  248. }
  249. CButton::OnLButtonUp(nFlags, point);
  250. }
  251. void CPinButton::OnKillFocus(CWnd* pNewWnd) 
  252. {
  253. CButton::OnKillFocus(pNewWnd);
  254. if (pNewWnd && pNewWnd != GetParent() && !(GetParent()->IsChild(pNewWnd)) && !bCloseLock)
  255. GetParent()->SendMessage(WM_KILLFOCUS, (WPARAM) pNewWnd);
  256. }
  257. void CPinButton::OnSetFocus(CWnd* pOldWnd) 
  258. {
  259. CButton::OnSetFocus(pOldWnd);
  260. // if (pOldWnd && pOldWnd != GetParent()) 
  261. // GetParent()->SetFocus();
  262. }