GfxOutBarCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #include "stdafx.h"
  2. #include "GfxOutBarCtrl.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. #define IDC_CURSOR_HAND 8899
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CGfxOutBarCtrl
  11. IMPLEMENT_DYNCREATE(CGfxOutBarCtrl, CWnd)
  12. CGfxOutBarCtrl::CGfxOutBarCtrl()
  13. {
  14. //背景色
  15. m_crBackground = RGB(255,255,255);
  16. //字体颜色
  17. m_crText = RGB(27,79,116);
  18. //字体
  19. m_pFont = NULL;
  20. //目录高度
  21. m_nFolderHeight = 20;
  22. //当前选中目录索引
  23. m_nCurSelFolder = 1;
  24. //最后高亮的目录索引
  25. iLastFolderHighlighted = -1;
  26. //当前鼠标滑过的目录
  27. iHoverFolder = -1;
  28. hHandCursor = AfxGetApp()->LoadCursor(IDC_CURSOR_HAND);
  29. iSelcon = NULL;
  30. }
  31. CGfxOutBarCtrl::~CGfxOutBarCtrl()
  32. {
  33. for (int t = 0; t < m_arFolder.GetSize(); t++)
  34. {
  35. if (m_arFolder.GetAt(t)) delete (CBm_arFolder*) m_arFolder.GetAt(t);
  36. }
  37. m_arFolder.RemoveAll();
  38. }
  39. BEGIN_MESSAGE_MAP(CGfxOutBarCtrl, CWnd)
  40. //{{AFX_MSG_MAP(CGfxOutBarCtrl)
  41. ON_WM_CREATE()
  42. ON_WM_PAINT()
  43. ON_WM_ERASEBKGND()
  44. ON_WM_LBUTTONDOWN()
  45. ON_WM_SETCURSOR()
  46. ON_WM_SIZE()
  47. //}}AFX_MSG_MAP
  48. ON_WM_MOUSEMOVE()
  49. ON_WM_TIMER()
  50. ON_WM_MOUSEWHEEL()
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CGfxOutBarCtrl message handlers
  54. void CGfxOutBarCtrl::SetFolderImage(LPCTSTR lpszPath)
  55. {
  56. CEnBitmap bmpTemp;
  57. bmpTemp.LoadImage(lpszPath);
  58. bmpTemp.DrawImage(m_bmpNormalFolder,1,1,1,2);
  59. bmpTemp.DrawImage(m_bmpHoverFolder,1,2,1,2);
  60. bmpTemp.DeleteObject();
  61. };
  62. void CGfxOutBarCtrl::SetSelIcon(LPCTSTR lpszIconPath)
  63. {
  64. iSelcon=(HICON)::LoadImage(AfxGetApp()->m_hInstance,lpszIconPath,IMAGE_ICON,16,16,LR_LOADFROMFILE);
  65. }
  66. void CGfxOutBarCtrl::SetFolderText(const int index, const TCHAR * text)
  67. {
  68. ASSERT(index >= 0 && index < GetFolderCount());
  69. CBm_arFolder * pbf = (CBm_arFolder *) m_arFolder.GetAt(index);
  70. if (pbf->cName)
  71. {
  72. delete [] pbf->cName;
  73. pbf->cName = NULL;
  74. }
  75. pbf->cName = new TCHAR [lstrlen(text)+1];
  76. lstrcpy(pbf->cName, text);
  77. }
  78. BOOL CGfxOutBarCtrl::Create(DWORD dwStyle, const RECT & rect, CWnd * pParentWnd, UINT nID)
  79. {
  80. return CWnd::CreateEx(0, NULL, NULL, dwStyle|WS_CHILD, rect, pParentWnd, nID);
  81. }
  82. int CGfxOutBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  83. {
  84. if (CWnd::OnCreate(lpCreateStruct) == -1)
  85. return -1;
  86. // TODO: Add your specialized creation code here
  87. return 0;
  88. }
  89. void CGfxOutBarCtrl::OnPaint()
  90. {
  91. CRect rc;
  92. GetClientRect(&rc);
  93. CPaintDC dc(this);
  94. CDC memDC;
  95. memDC.CreateCompatibleDC(&dc);
  96. CBitmap bmp;
  97. bmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());
  98. CBitmap * oldbmp = memDC.SelectObject(&bmp);;
  99. memDC.FillSolidRect(rc,m_crBackground);
  100. int nFolders=m_arFolder.GetSize();
  101. for(int i=0;i<nFolders;i++)
  102. {
  103. CRect rcFolder;
  104. GetFolderRect(i,rcFolder);
  105. DrawFolder(&memDC,i,&rcFolder);
  106. }
  107. dc.BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),&memDC,0,0,SRCCOPY);//Copy
  108. memDC.DeleteDC();
  109. }
  110. BOOL CGfxOutBarCtrl::OnEraseBkgnd(CDC* pDC)
  111. {
  112. return true;
  113. }
  114. bool CGfxOutBarCtrl::GetFolderRect(const int iIndex, CRect & rect) const
  115. {
  116. int max = m_arFolder.GetSize();
  117. ASSERT(iIndex >= 0 && iIndex < max);
  118. if (iIndex >= 0 && iIndex < max)
  119. {
  120. CRect rc;
  121. GetClientRect(rc);
  122. if (iIndex > m_nCurSelFolder)
  123. rect.SetRect(rc.left, rc.bottom - ((max - iIndex)) * m_nFolderHeight, rc.right,
  124. rc.bottom - (max - iIndex - 1) * m_nFolderHeight);
  125. else
  126. rect.SetRect(rc.left, rc.top + iIndex * m_nFolderHeight, rc.right,
  127. rc.top + (1 + iIndex) * m_nFolderHeight);
  128. return true;
  129. }
  130. return false;
  131. }
  132. bool CGfxOutBarCtrl::PtInFolderHeadRect(CPoint pt) const
  133. {
  134. int max = m_arFolder.GetSize();
  135. ASSERT(m_nCurSelFolder >= 0 && m_nCurSelFolder < max);
  136. CRect rcClient;
  137. GetClientRect(rcClient);
  138. CRect rcTop, rcBottom;
  139. rcTop.SetRect(rcClient.left, rcClient.top, rcClient.right, rcClient.top + (m_nCurSelFolder + 1) * m_nFolderHeight);
  140. rcBottom.SetRect(rcClient.left, rcClient.bottom - ( max - m_nCurSelFolder) * m_nFolderHeight, rcClient.right, rcClient.bottom );
  141. if ( rcTop.PtInRect(pt) || rcBottom.PtInRect(pt) )
  142. return true;
  143. return false;
  144. }
  145. int CGfxOutBarCtrl::AddFolder(const TCHAR * cFolderName, const DWORD exData)
  146. {
  147. CBm_arFolder * pbf = new CBm_arFolder(cFolderName, exData);
  148. ASSERT(pbf);
  149. m_arFolder.Add((void *)pbf);
  150. return m_arFolder.GetSize() - 1;
  151. }
  152. int CGfxOutBarCtrl::AddFolderBar(const TCHAR * pFolder, CWnd * pSon, const DWORD exData)
  153. {
  154. CBm_arFolder * pbf = new CBm_arFolder(pFolder, exData);
  155. ASSERT(pbf);
  156. pbf->pChild = pSon;
  157. m_arFolder.Add((void *)pbf);
  158. return m_arFolder.GetSize() - 1;
  159. }
  160. CGfxOutBarCtrl::CBm_arFolder::CBm_arFolder(const TCHAR * name, DWORD exData)
  161. {
  162. cName = NULL;
  163. dwData = exData;
  164. if (name)
  165. {
  166. cName = new TCHAR[lstrlen(name)+1];
  167. ASSERT(cName);
  168. lstrcpy(cName, name);
  169. }
  170. pChild = NULL;
  171. }
  172. CGfxOutBarCtrl::CBm_arFolder::~CBm_arFolder()
  173. {
  174. if (cName) delete [] cName;
  175. }
  176. void CGfxOutBarCtrl::GetInsideRect(CRect & rect) const
  177. {
  178. GetClientRect(rect);
  179. if (m_arFolder.GetSize() > 0)
  180. {
  181. int max = m_arFolder.GetSize();
  182. rect.top += m_nFolderHeight * (m_nCurSelFolder + 1);//+ 2;
  183. rect.bottom -= (max - m_nCurSelFolder - 1)*m_nFolderHeight +1;
  184. return;
  185. }
  186. }
  187. int CGfxOutBarCtrl::HitTestEx(const CPoint & point, int &index)
  188. {
  189. int max = m_arFolder.GetSize(), t;
  190. CRect rc;
  191. for (t = 0; t < max; t++)
  192. {
  193. GetFolderRect(t, rc);
  194. if (rc.PtInRect(point))
  195. {
  196. index = t;
  197. return htFolder;
  198. }
  199. }
  200. index = -1;
  201. return htNothing;
  202. }
  203. void CGfxOutBarCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  204. {
  205. if (GetFocus() != this) SetFocus();
  206. int index, ht = HitTestEx(point, index);
  207. if (ht == htFolder)
  208. {
  209. bool bHigh = true;
  210. if (::GetCapture() == NULL)
  211. {
  212. SetCapture();
  213. ASSERT(this == GetCapture());
  214. AfxLockTempMaps();
  215. while (TRUE)
  216. {
  217. MSG msg;
  218. VERIFY(::GetMessage(&msg, NULL, 0, 0));
  219. if (CWnd::GetCapture() != this) break;
  220. switch (msg.message)
  221. {
  222. case WM_LBUTTONUP:
  223. {
  224. CPoint pt(msg.lParam);
  225. int idx, ht1 = HitTestEx(pt, idx);
  226. if (ht1 == htFolder && idx != m_nCurSelFolder)
  227. {
  228. //idx = (idx == m_nCurSelFolder)?(idx+1):idx;
  229. if(idx <GetFolderCount())
  230. SetSelFolder(idx);
  231. }
  232. }
  233. goto ExitLoop;
  234. default:
  235. DispatchMessage(&msg);
  236. break;
  237. }
  238. }
  239. ExitLoop:
  240. ReleaseCapture();
  241. AfxUnlockTempMaps(FALSE);
  242. }
  243. }
  244. CWnd::OnLButtonDown(nFlags, point);
  245. }
  246. void CGfxOutBarCtrl::SetSelFolder(const int index)
  247. {
  248. ASSERT(index >= 0 && index < GetFolderCount());
  249. if (index == m_nCurSelFolder) return;
  250. CWnd * pc = GetFolderChild();
  251. if (pc) pc->ShowWindow(SW_HIDE);
  252. m_nCurSelFolder = index;
  253. pc = GetFolderChild();
  254. if (pc)
  255. {
  256. CRect rc;
  257. GetInsideRect(rc);
  258. pc->MoveWindow(rc);
  259. pc->ShowWindow(SW_SHOW);
  260. }
  261. Invalidate();
  262. }
  263. int CGfxOutBarCtrl::GetFolderCount() const
  264. {
  265. return m_arFolder.GetSize();
  266. }
  267. int CGfxOutBarCtrl::GetSelFolder() const
  268. {
  269. return m_nCurSelFolder;
  270. }
  271. void CGfxOutBarCtrl::RemoveFolder(const int index)
  272. {
  273. ASSERT(index >= 0 && index < GetFolderCount());
  274. CBm_arFolder * pbf = (CBm_arFolder *) m_arFolder.GetAt(index);
  275. delete pbf;
  276. m_arFolder.RemoveAt(index);
  277. if (m_nCurSelFolder >= index) m_nCurSelFolder = index - 1;
  278. if (m_nCurSelFolder < 0 && GetFolderCount() > 0) m_nCurSelFolder = 0;
  279. Invalidate();
  280. }
  281. BOOL CGfxOutBarCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  282. {
  283. CPoint pt(GetMessagePos());
  284. ScreenToClient(&pt);
  285. int index, ht = HitTestEx(pt, index);
  286. if (ht == htFolder)
  287. {
  288. SetCursor(hHandCursor);
  289. return true;
  290. }
  291. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  292. }
  293. void CGfxOutBarCtrl::OnSize(UINT nType, int cx, int cy)
  294. {
  295. CWnd::OnSize(nType, cx, cy);
  296. int t, max = GetFolderCount();
  297. CRect rc;
  298. GetInsideRect(rc);
  299. for (t = 0; t < max; t++)
  300. {
  301. CBm_arFolder * pbf = (CBm_arFolder *) m_arFolder.GetAt(t);
  302. CWnd * pc = GetFolderChild(t);
  303. if (pc) pc->SetWindowPos(0, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER);
  304. }
  305. }
  306. CWnd * CGfxOutBarCtrl::GetFolderChild(int iFolder)
  307. {
  308. if (GetFolderCount())
  309. {
  310. if (iFolder < 0) iFolder = m_nCurSelFolder;
  311. CBm_arFolder * pbf = (CBm_arFolder *) m_arFolder.GetAt(iFolder);
  312. return pbf->pChild;
  313. }
  314. return NULL;
  315. }
  316. void CGfxOutBarCtrl::SetAutoFolder(IN const DWORD& dwTime)
  317. {
  318. SetTimer(2, dwTime, NULL);
  319. }
  320. DWORD CGfxOutBarCtrl::GetFolderData(int iFolder)
  321. {
  322. if (iFolder < 0) iFolder = m_nCurSelFolder;
  323. CBm_arFolder * pbf = (CBm_arFolder *) m_arFolder.GetAt(iFolder);
  324. return pbf->dwData;
  325. }
  326. void CGfxOutBarCtrl::OnMouseMove(UINT nFlags, CPoint point)
  327. {
  328. // TODO: 在此添加消息处理程序代码和/或调用默认值
  329. if (GetFocus() != this && PtInFolderHeadRect(point))
  330. SetFocus();
  331. int ht = HitTestEx(point, iHoverFolder);
  332. if (ht == htFolder)
  333. {
  334. HighlightFolder(iHoverFolder);
  335. SetTimer(1,100,NULL);
  336. }
  337. m_curpoint = point;
  338. CWnd::OnMouseMove(nFlags, point);
  339. }
  340. void CGfxOutBarCtrl::DrawFolder(CDC * pDC, const int iIdx, CRect rect)
  341. {
  342. if(iIdx < 0) return;
  343. CBm_arFolder *pBf=(CBm_arFolder*)m_arFolder.GetAt(iIdx);
  344. if (iHoverFolder == iIdx)
  345. {
  346. if(m_bmpHoverFolder.m_hObject != NULL)
  347. m_bmpHoverFolder.ExtendDraw(pDC,rect,20,5);
  348. }
  349. else
  350. {
  351. if(m_bmpNormalFolder.m_hObject != NULL)
  352. m_bmpNormalFolder.ExtendDraw(pDC,rect,20,5);
  353. }
  354. if(m_nCurSelFolder == iIdx && iSelcon != NULL)
  355. {
  356. ::DrawIconEx(*pDC,rect.left+2,rect.top +(m_nFolderHeight-16)/2,iSelcon,16,16,0,NULL,DI_NORMAL);
  357. }
  358. //Draw Folder Name In Folder
  359. if(m_pFont == NULL)
  360. m_pFont = CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT));
  361. pDC->SelectObject(m_pFont);
  362. pDC->SetTextColor(m_crText);
  363. pDC->SetBkMode(TRANSPARENT);
  364. pDC->DrawText(pBf->cName,rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  365. }
  366. void CGfxOutBarCtrl::HighlightFolder(const int index)
  367. {
  368. if (iLastFolderHighlighted == index) return;
  369. CClientDC dc(this);
  370. if (iLastFolderHighlighted >= 0)
  371. {
  372. CRect rc;
  373. if (GetFolderRect(iLastFolderHighlighted, rc))
  374. {
  375. DrawFolder(&dc,iLastFolderHighlighted,rc);
  376. }
  377. }
  378. if (index >= 0)
  379. {
  380. CRect rc;
  381. if (GetFolderRect(index, rc))
  382. {
  383. DrawFolder(&dc,index,rc);
  384. }
  385. }
  386. iLastFolderHighlighted = index;
  387. }
  388. void CGfxOutBarCtrl::OnTimer(UINT_PTR nIDEvent)
  389. {
  390. // TODO: 在此添加消息处理程序代码和/或调用默认值
  391. if (nIDEvent == 1)
  392. {
  393. CPoint pt(GetMessagePos());
  394. ScreenToClient(&pt);
  395. CRect rc; GetClientRect(&rc);
  396. int ht = HitTestEx(pt, iHoverFolder);
  397. if (ht != htFolder || !(rc.PtInRect(pt)))
  398. {
  399. HighlightFolder(-1);
  400. KillTimer(1);
  401. }
  402. }
  403. if ( nIDEvent == 2 )
  404. {// 鼠标在folderheader上x毫秒自动打开;
  405. if (GetFocus() != this && PtInFolderHeadRect(m_curpoint)) SetFocus();
  406. int index, ht = HitTestEx(m_curpoint, index);
  407. if (ht == htFolder)
  408. {
  409. bool bHigh = true;
  410. if (::GetCapture() == NULL)
  411. {
  412. SetCapture();
  413. ASSERT(this == GetCapture());
  414. AfxLockTempMaps();
  415. if(index <GetFolderCount())
  416. SetSelFolder(index);
  417. ReleaseCapture();
  418. AfxUnlockTempMaps(FALSE);
  419. }
  420. }
  421. }
  422. CWnd::OnTimer(nIDEvent);
  423. }
  424. BOOL CGfxOutBarCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  425. {
  426. if (GetFocus() != this)
  427. SetFocus();
  428. if ( m_nCurSelFolder <= 0 && m_nCurSelFolder >= m_arFolder.GetSize() )
  429. return FALSE;
  430. ScreenToClient(&pt);
  431. if ( !PtInFolderHeadRect(pt) )
  432. {
  433. return FALSE;
  434. }
  435. INT nIndex = m_nCurSelFolder;
  436. if (::GetCapture() == NULL)
  437. {
  438. SetCapture();
  439. AfxLockTempMaps();
  440. CBm_arFolder *pChild = NULL;
  441. if ( zDelta > 0 )
  442. {// 向上滚动;
  443. if ( m_nCurSelFolder == 0 )
  444. {
  445. ReleaseCapture();
  446. AfxUnlockTempMaps(FALSE);
  447. return TRUE;
  448. }
  449. nIndex--;
  450. pChild = (CBm_arFolder *)m_arFolder.GetAt(nIndex);
  451. if ( pChild )
  452. {
  453. SetSelFolder(nIndex);
  454. }
  455. }
  456. else
  457. {// 向下滚动;
  458. if ( m_nCurSelFolder == GetFolderCount() - 1)
  459. {
  460. ReleaseCapture();
  461. AfxUnlockTempMaps(FALSE);
  462. return TRUE;
  463. }
  464. nIndex++;
  465. pChild = (CBm_arFolder *)m_arFolder.GetAt(nIndex);
  466. if ( pChild )
  467. {
  468. SetSelFolder(nIndex);
  469. }
  470. }
  471. ReleaseCapture();
  472. AfxUnlockTempMaps(FALSE);
  473. }
  474. Invalidate();
  475. return CWnd::OnMouseWheel(nFlags, zDelta, pt);
  476. }