TRACKLOOKBUTTON.CPP
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:14k
源码类别:

SNMP编程

开发平台:

C/C++

  1. // CTrackLookButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TrackLookButton.h"
  5. #include "MemDC.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define RGB_BUTTON_BLACK    (GetSysColor(COLOR_WINDOWFRAME))
  12. #define RGB_BUTTON_WHITE    (GetSysColor(COLOR_BTNHIGHLIGHT))
  13. #define RGB_BUTTON_LIGHT    (GetSysColor(COLOR_BTNFACE))
  14. #define RGB_BUTTON_DARK     (GetSysColor(COLOR_BTNSHADOW))
  15. #define BORDER_CLEAR 0x0000L
  16. #define BORDER_PUSHED 0x0001L
  17. #define BORDER_NONPUSHED 0x0002L
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CTrackLookButton
  20. CTrackLookButton::CTrackLookButton()
  21. {
  22. m_bMouseCaptured=FALSE;
  23. m_bLButtonDown=FALSE;
  24. m_bHasFocus=FALSE;
  25. m_bDisabled=FALSE;
  26. m_TextAlign=AlignLeft;
  27. m_nBorder=BORDER_CLEAR;
  28. m_bRaised=FALSE;
  29. }
  30. CTrackLookButton::~CTrackLookButton()
  31. {
  32. }
  33. BEGIN_MESSAGE_MAP(CTrackLookButton, CButton)
  34. //{{AFX_MSG_MAP(CTrackLookButton)
  35. ON_WM_MOUSEMOVE()
  36. ON_WM_KILLFOCUS()
  37. ON_WM_SETFOCUS()
  38. ON_WM_LBUTTONDOWN()
  39. ON_WM_LBUTTONUP()
  40. ON_WM_LBUTTONDBLCLK()
  41. ON_WM_CREATE()
  42. ON_WM_ENABLE()
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CTrackLookButton message handlers
  47. void CTrackLookButton::OnMouseMove(UINT nFlags, CPoint point) 
  48. {
  49. CButton::OnMouseMove(nFlags,point);
  50. if (!m_bMouseCaptured || GetCapture()!=this || m_nBorder==BORDER_CLEAR)
  51. {
  52. SetCapture();
  53. m_bMouseCaptured=TRUE;
  54. OnMouseEnter(nFlags,point);
  55. } else
  56. {
  57. CRect rc;
  58. this->GetClientRect(&rc);
  59. if (!rc.PtInRect(point))
  60. {
  61. OnMouseLeave(nFlags,point);
  62. m_bMouseCaptured=FALSE;
  63. ReleaseCapture();
  64. }
  65. }
  66. }
  67. void CTrackLookButton::OnKillFocus(CWnd* pNewWnd) 
  68. {
  69. m_nBorder=BORDER_CLEAR;
  70. m_bHasFocus=FALSE;
  71. m_bRaised=FALSE;
  72. CButton::OnKillFocus(pNewWnd);
  73. Invalidate();
  74. UpdateWindow();
  75. }
  76. void CTrackLookButton::OnSetFocus(CWnd* pOldWnd) 
  77. {
  78. m_nBorder=m_bLButtonDown?BORDER_PUSHED:BORDER_NONPUSHED;
  79. m_bHasFocus=TRUE;
  80. m_bRaised=TRUE;
  81. CButton::OnSetFocus(pOldWnd);
  82. Invalidate();
  83. UpdateWindow();
  84. }
  85. void CTrackLookButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  86. {
  87. int  iSaveDC;
  88. CDC* pDC;
  89. CBrush brush(RGB_BUTTON_LIGHT);
  90. CRect rc;
  91. CString strTitle;
  92. UINT nFormat; //For DrawText
  93. UINT nFlags; //For DrawBitmap
  94. CBitmap* pBitmap=NULL;
  95. pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  96. VERIFY(pDC);
  97. rc.CopyRect(&lpDrawItemStruct->rcItem);
  98. GetWindowText(strTitle);
  99. nFormat=DT_SINGLELINE;
  100. iSaveDC=pDC->SaveDC();
  101. switch (m_TextAlign)
  102. {
  103. case AlignAbove: nFormat|=DT_CENTER|DT_TOP;break;
  104. case AlignBelow: nFormat|=DT_CENTER|DT_BOTTOM;break;
  105. case AlignLeft:
  106. case AlignRight: nFormat|=DT_LEFT|DT_VCENTER;break;
  107. default: ASSERT(FALSE); // should not be called;
  108. }
  109. pDC->SetBkMode(TRANSPARENT);
  110. Draw3DBorder(pDC,rc,m_nBorder);
  111. rc.InflateRect(-5,-2); //We assert we have a 5 points offset from border
  112. if (m_bHasFocus)
  113. {
  114. pDC->SetTextColor(RGB(0,0,255));
  115. pBitmap=&m_bitmapFocus;
  116. if (pBitmap->m_hObject==NULL) pBitmap=&m_bitmap; //Simulate some bitmap;
  117. }
  118. else
  119. {
  120. pDC->SetTextColor(RGB_BUTTON_BLACK);
  121. pBitmap=&m_bitmap;
  122. }
  123. if (m_bRaised)
  124. {
  125. pBitmap=&m_bitmapFocus;
  126. if (pBitmap->m_hObject==NULL) pBitmap=&m_bitmap; //Simulate some bitmap;
  127. if (m_bDisabled=(::GetWindowLong(m_hWnd,GWL_STYLE) & WS_DISABLED))
  128. {
  129. pBitmap=&m_bitmapDisabled;
  130. }
  131. else if (m_bLButtonDown) rc.OffsetRect(1,1);
  132. CRect rcText(rc);
  133. if (pBitmap->m_hObject)
  134. {
  135. CRect rcBitmap(rc);
  136. BITMAP bmpInfo;
  137. CSize size;
  138. switch (m_TextAlign)
  139. {
  140. case AlignLeft:
  141. {
  142. size=pDC->GetTextExtent(strTitle);
  143. rcBitmap.OffsetRect(size.cx+5,0);
  144. nFlags=DB_VCENTER;
  145. break;
  146. }
  147. case AlignAbove:
  148. {
  149. size=pDC->GetTextExtent(strTitle);
  150. rcBitmap.OffsetRect(0,size.cy+5);
  151. nFlags=DB_HCENTER;
  152. break;
  153. }
  154. case AlignRight:
  155. {
  156. pBitmap->GetBitmap(&bmpInfo);
  157. rcText.OffsetRect(bmpInfo.bmWidth+5,0);
  158. nFlags=DB_VCENTER;
  159. break;
  160. }
  161. case AlignBelow:
  162. {
  163. nFlags=DB_HCENTER;
  164. break;
  165. }
  166. default: ASSERT(FALSE);break;
  167. }
  168. DrawBitmap(pDC,rcBitmap,nFlags,pBitmap);
  169. }
  170. if (m_bDisabled)
  171. {
  172. rcText.OffsetRect(1,1);
  173. pDC->SetTextColor(RGB_BUTTON_WHITE);
  174. pDC->DrawText(strTitle,rcText,nFormat);
  175. rcText.OffsetRect(-1,-1);
  176. pDC->SetTextColor(RGB_BUTTON_DARK);
  177. pDC->DrawText(strTitle,rcText,nFormat);
  178. } else
  179. pDC->DrawText(strTitle,rcText,nFormat);
  180. pDC->RestoreDC(iSaveDC);
  181. }
  182. void CTrackLookButton::OnMouseEnter(UINT nFlags,CPoint point)
  183. {
  184. m_bLButtonDown=(nFlags & MK_LBUTTON);
  185. m_nBorder=m_bLButtonDown?BORDER_PUSHED:BORDER_NONPUSHED;
  186. //if (m_bLButtonDown)
  187. //{
  188. m_bRaised=TRUE;
  189. Invalidate();
  190. UpdateWindow();
  191. }
  192. void CTrackLookButton::OnMouseLeave(UINT nFlags,CPoint point)
  193. {
  194. m_nBorder=BORDER_CLEAR;
  195. m_bLButtonDown=FALSE;
  196. m_bRaised=FALSE;
  197. Invalidate();
  198. UpdateWindow();
  199. }
  200. void CTrackLookButton::Draw3DBorder(CDC* pDC,CRect rc,UINT nOptions)
  201. {
  202. switch (nOptions)
  203. {
  204. case BORDER_CLEAR:
  205. pDC->Draw3dRect(rc,RGB_BUTTON_LIGHT,RGB_BUTTON_LIGHT);
  206. break;
  207. case BORDER_PUSHED:
  208. pDC->Draw3dRect(rc,RGB_BUTTON_DARK,RGB_BUTTON_WHITE);
  209. break;
  210. case BORDER_NONPUSHED:
  211. pDC->Draw3dRect(rc,RGB_BUTTON_WHITE,RGB_BUTTON_DARK);
  212. break;
  213. default: break;
  214. }
  215. }
  216. void CTrackLookButton::OnLButtonDown(UINT nFlags, CPoint point) 
  217. {
  218. m_bLButtonDown=TRUE;
  219. m_bRaised=FALSE;
  220. if (GetFocus()!=this)
  221. {
  222. this->SetFocus();
  223. return;
  224. }
  225. m_nBorder=BORDER_PUSHED;
  226. CButton::OnLButtonDown(nFlags,point);
  227. Invalidate();
  228. UpdateWindow();
  229. }
  230. void CTrackLookButton::OnLButtonUp(UINT nFlags, CPoint point) 
  231. {
  232. m_bLButtonDown=FALSE;
  233. m_bRaised=TRUE;
  234. if (GetFocus()!=this)
  235. {
  236. this->SetFocus();
  237. } else
  238. {
  239. m_nBorder=BORDER_NONPUSHED;
  240. Invalidate();
  241. UpdateWindow();
  242. }
  243. GetParent()->SendMessage(WM_COMMAND,
  244. MAKEWPARAM(GetDlgCtrlID(),BN_CLICKED),
  245. (LPARAM) m_hWnd);
  246. }
  247. void CTrackLookButton::OnLButtonDblClk(UINT nFlags, CPoint point) 
  248. {
  249. m_bLButtonDown=TRUE;
  250. if (GetFocus()!=this)
  251. {
  252. this->SetFocus();
  253. return;
  254. }
  255. m_nBorder=BORDER_PUSHED;
  256. Invalidate();
  257. UpdateWindow();
  258. CButton::OnLButtonDblClk(nFlags,point);
  259. m_nBorder=BORDER_CLEAR;
  260. }
  261. int CTrackLookButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  262. {
  263. if (CButton::OnCreate(lpCreateStruct) == -1)
  264. return -1;
  265. m_bDisabled=(lpCreateStruct->style & WS_DISABLED);
  266. return 0;
  267. }
  268. void CTrackLookButton::OnEnable(BOOL bEnable) 
  269. {
  270. m_bDisabled=!bEnable;
  271. CButton::OnEnable(bEnable);
  272. }
  273. BOOL CTrackLookButton::LoadBitmaps(UINT nBitmap, UINT nBitmapFocus, UINT nBitmapDisabled)
  274. {
  275. return LoadBitmaps(MAKEINTRESOURCE(nBitmap),
  276. MAKEINTRESOURCE(nBitmapFocus),
  277. MAKEINTRESOURCE(nBitmapDisabled));
  278. }
  279. CTrackLookButton::TextAlign CTrackLookButton::GetTextAlignment() const
  280. {
  281. return m_TextAlign;
  282. }
  283. void CTrackLookButton::SetTextAlignment(TextAlign nTextAlign)
  284. {
  285. m_TextAlign=nTextAlign;
  286. }
  287. void CTrackLookButton::DrawBitmap(CDC * pDC, CRect rc,UINT nFlags,CBitmap * pBitmap)
  288. {
  289. //Centers a bitmap in a given rectangle
  290. //If necessary clips the bitmap if outfits the rc
  291. CDC memDC;
  292. CBitmap* pOld=NULL;
  293. memDC.CreateCompatibleDC(pDC);
  294. BITMAP bmpInfo;
  295. int Width;
  296. int Height;
  297. int xSrc=0;
  298. int ySrc=0;
  299. int xDesired;
  300. int yDesired;
  301. ASSERT(pBitmap->m_hObject!=NULL);
  302. pBitmap->GetBitmap(&bmpInfo);
  303. pOld=memDC.SelectObject((CBitmap*) pBitmap);
  304. if (pOld==NULL) return; //Destructors will clean up
  305. Width=(bmpInfo.bmWidth-rc.Width())/2;
  306. Height=(bmpInfo.bmHeight-rc.Height())/2;
  307. if ((nFlags & DB_HCENTER))
  308. {
  309. if (Width>0) //If the bitmap Width is larger then rc
  310. {
  311. xDesired=rc.left;
  312. xSrc=abs(Width);
  313. }
  314. else xDesired=rc.left+ abs(Width);
  315. }
  316. else xDesired=rc.left;
  317. if ((nFlags & DB_VCENTER))
  318. {
  319. if (Height>0) //If the bitmap Height is larger then rc
  320. {
  321. yDesired=rc.top;
  322. ySrc=abs(Height);
  323. }
  324. else yDesired=rc.top+abs(Height);
  325. } else
  326. yDesired=rc.top;
  327. pDC->BitBlt(xDesired,yDesired,rc.Width(),rc.Height(),&memDC,xSrc,ySrc,SRCCOPY);
  328. memDC.SelectObject(pOld);
  329. }
  330. BOOL CTrackLookButton::LoadBitmaps(LPCSTR lpszBitmap, LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled,BOOL bFromFile)
  331. {
  332. //Delete old ones
  333. m_bitmap.DeleteObject();
  334. m_bitmapFocus.DeleteObject();
  335. m_bitmapDisabled.DeleteObject();
  336. m_bitmapRaised.DeleteObject();
  337. if(bFromFile)
  338. {
  339. m_hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),lpszBitmap,
  340. IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  341. if(m_hBitmap == NULL)
  342. return FALSE;
  343. m_bitmap.Attach(m_hBitmap);
  344. if(GetSafeHwnd())
  345. {
  346. BITMAP bm;
  347. GetObject(m_hBitmap,sizeof(BITMAP),&bm);
  348. SetWindowPos(NULL,0,0,bm.bmWidth,bm.bmHeight,SWP_NOMOVE|SWP_NOZORDER);
  349. }
  350. BOOL bAllLoaded=TRUE;
  351. if (lpszBitmapFocus!=NULL)
  352. {
  353. m_hBitmapFocus = (HBITMAP)LoadImage(AfxGetInstanceHandle(),lpszBitmapFocus,
  354. IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  355. if(m_hBitmapFocus == NULL)
  356. return FALSE;
  357. m_bitmapFocus.Attach(m_hBitmapFocus);
  358. }
  359. if (lpszBitmapDisabled!=NULL)
  360. {
  361. m_hBitmapDisabled = (HBITMAP)LoadImage(AfxGetInstanceHandle(),lpszBitmapDisabled,
  362. IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  363. if(m_hBitmapDisabled == NULL)
  364. return FALSE;
  365. m_bitmapDisabled.Attach(m_hBitmapDisabled);
  366. }
  367. return bAllLoaded;
  368. }
  369. if (!m_bitmap.LoadBitmap(lpszBitmap))
  370. {
  371. TRACE0("Failed to Load First bitmap of CButtonn");
  372. return FALSE;
  373. }
  374. BOOL bAllLoaded=TRUE;
  375. if (lpszBitmapFocus!=NULL)
  376. {
  377. if (!m_bitmapFocus.LoadBitmap(lpszBitmapFocus))
  378. {
  379. TRACE0("Failed to Load First bitmap of CButtonn");
  380. return bAllLoaded=FALSE;
  381. }
  382. }
  383. if (lpszBitmapDisabled!=NULL)
  384. {
  385. if (!m_bitmapDisabled.LoadBitmap(lpszBitmapDisabled))
  386. {
  387. TRACE0("Failed to Load bitmap of CButtonn");
  388. return bAllLoaded=FALSE;
  389. }
  390. }
  391. return bAllLoaded;
  392. }
  393. /////////////////////////////////////////////////////////////////////////////
  394. // CTrackLookButtonEx
  395. CTrackLookButtonEx::CTrackLookButtonEx()
  396. {
  397. }
  398. CTrackLookButtonEx::~CTrackLookButtonEx()
  399. {
  400. }
  401. BEGIN_MESSAGE_MAP(CTrackLookButtonEx, CTrackLookButton)
  402. //{{AFX_MSG_MAP(CTrackLookButtonEx)
  403. // NOTE - the ClassWizard will add and remove mapping macros here.
  404. //}}AFX_MSG_MAP
  405. END_MESSAGE_MAP()
  406. /////////////////////////////////////////////////////////////////////////////
  407. // CTrackLookButtonEx message handlers
  408. void CTrackLookButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  409. {
  410. // TODO: Add your code to draw the specified item
  411. int  iSaveDC;
  412. CDC* pDC;
  413. CBrush brush(RGB_BUTTON_LIGHT);
  414. CRect rc;
  415. CString strTitle;
  416. UINT nFormat; //For DrawText
  417. UINT nFlags; //For DrawBitmap
  418. CBitmap* pBitmap=NULL;
  419. pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  420. VERIFY(pDC);
  421. rc.CopyRect(&lpDrawItemStruct->rcItem);
  422. GetWindowText(strTitle);
  423. nFormat=DT_SINGLELINE;
  424. iSaveDC=pDC->SaveDC();
  425. switch (m_TextAlign)
  426. {
  427. case AlignAbove: nFormat|=DT_CENTER|DT_TOP;break;
  428. case AlignBelow: nFormat|=DT_CENTER|DT_BOTTOM;break;
  429. case AlignLeft:nFormat|=DT_CENTER;break;
  430. case AlignRight: nFormat|=DT_LEFT|DT_VCENTER;break;
  431. default: ASSERT(FALSE); // should not be called;
  432. }
  433. pDC->SetBkMode(TRANSPARENT);
  434. CFont * pFont = GetFont();
  435. pDC->SelectObject(pFont);
  436. Draw3DBorder(pDC,rc,m_nBorder);
  437. // rc.InflateRect(-5,-2); //We assert we have a 5 points offset from border
  438. if (m_bHasFocus)
  439. {
  440. pDC->SetTextColor(RGB(0,255,0));
  441. pBitmap=&m_bitmapFocus;
  442. if (pBitmap->m_hObject==NULL) pBitmap=&m_bitmap; //Simulate some bitmap;
  443. }
  444. else
  445. {
  446. pDC->SetTextColor(RGB(255,255,255));//RGB_BUTTON_BLACK);
  447. pBitmap=&m_bitmap;
  448. }
  449. if (m_bRaised)
  450. {
  451. pDC->SetTextColor(RGB(255,0,0));
  452. pBitmap=&m_bitmapFocus;
  453. if (pBitmap->m_hObject==NULL) pBitmap=&m_bitmap; //Simulate some bitmap;
  454. if (m_bDisabled=(::GetWindowLong(m_hWnd,GWL_STYLE) & WS_DISABLED))
  455. {
  456. pBitmap=&m_bitmapDisabled;
  457. if (pBitmap->m_hObject==NULL) pBitmap=&m_bitmap; //Simulate some bitmap;
  458. }
  459. // else if (m_bLButtonDown) rc.OffsetRect(1,1);
  460. CRect rcText(rc);
  461. if (pBitmap->m_hObject)
  462. {
  463. CRect rcBitmap(rc);
  464. BITMAP bmpInfo;
  465. CSize size;
  466. switch (m_TextAlign)
  467. {
  468. case AlignLeft:
  469. {
  470. size=pDC->GetTextExtent(strTitle);
  471. rcBitmap.OffsetRect(size.cx+5,0);
  472. nFlags=DB_VCENTER;
  473. break;
  474. }
  475. case AlignAbove:
  476. {
  477. size=pDC->GetTextExtent(strTitle);
  478. rcBitmap.OffsetRect(0,size.cy+5);
  479. nFlags=DB_HCENTER;
  480. break;
  481. }
  482. case AlignRight:
  483. {
  484. pBitmap->GetBitmap(&bmpInfo);
  485. // rcText.OffsetRect(bmpInfo.bmWidth+5,0);
  486. size=pDC->GetTextExtent(strTitle);
  487. rcText.OffsetRect((rc.Width()-size.cx)/2,0);
  488. nFlags=DB_VCENTER;
  489. break;
  490. }
  491. case AlignBelow:
  492. {
  493. nFlags=DB_HCENTER;
  494. break;
  495. }
  496. default: ASSERT(FALSE);break;
  497. }
  498. DrawBitmap(pDC,rcBitmap,nFlags,pBitmap);
  499. }
  500. if (m_bRaised)
  501. {
  502. // pDC->SetTextColor(RGB(255,0,0));
  503. else
  504. {
  505. COLORREF rgb = pDC->SetTextColor(RGB(0,0,0));
  506. rcText.OffsetRect(-1,-1);
  507. pDC->DrawText(strTitle,rcText,nFormat);
  508. rcText.OffsetRect(1,1);
  509. pDC->SetTextColor(rgb);//RGB_BUTTON_BLACK);
  510. }
  511. if (m_bDisabled)
  512. {
  513. rcText.OffsetRect(1,1);
  514. pDC->SetTextColor(RGB_BUTTON_WHITE);
  515. pDC->DrawText(strTitle,rcText,nFormat);
  516. rcText.OffsetRect(-1,-1);
  517. pDC->SetTextColor(RGB_BUTTON_DARK);
  518. pDC->DrawText(strTitle,rcText,nFormat);
  519. } else
  520. pDC->DrawText(strTitle,rcText,nFormat);
  521. if (m_bHasFocus) pDC->DrawFocusRect(rc);
  522. pDC->RestoreDC(iSaveDC);
  523. }
  524. void CTrackLookButtonEx::OnMouseEnter(UINT nFlags,CPoint point)
  525. {
  526. m_bLButtonDown=(nFlags & MK_LBUTTON);
  527. m_nBorder=m_bLButtonDown?BORDER_PUSHED:BORDER_NONPUSHED;
  528. //if (m_bLButtonDown)
  529. //{
  530. m_bRaised=TRUE;
  531. InvalidateRect(CRect(10,2,100,28));
  532. UpdateWindow();
  533. }
  534. void CTrackLookButtonEx::OnMouseLeave(UINT nFlags,CPoint point)
  535. {
  536. m_nBorder=BORDER_CLEAR;
  537. m_bLButtonDown=FALSE;
  538. m_bRaised=FALSE;
  539. InvalidateRect(CRect(10,2,100,28));
  540. UpdateWindow();
  541. }