FileView.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // ("Fluent UI"),该示例仅作为参考资料提供,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // http://msdn.microsoft.com/officeui。
  8. //
  9. // 版权所有 (C) Microsoft Corporation
  10. // 保留所有权利。
  11. #include "stdafx.h"
  12. #include "mainfrm.h"
  13. #include "FileView.h"
  14. #include "Resource.h"
  15. #include "IDE.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CFileView
  23. CFileView::CFileView()
  24. {
  25. }
  26. CFileView::~CFileView()
  27. {
  28. OutputDebugString(_T("侧边栏关闭\n"));
  29. }
  30. BEGIN_MESSAGE_MAP(CFileView, CDockablePane)
  31. ON_WM_CREATE()
  32. ON_WM_SIZE()
  33. ON_WM_CONTEXTMENU()
  34. ON_COMMAND(ID_PROPERTIES, OnProperties)
  35. ON_COMMAND(ID_OPEN, OnFileOpen)
  36. ON_COMMAND(ID_OPEN_WITH, OnFileOpenWith)
  37. ON_COMMAND(ID_DUMMY_COMPILE, OnDummyCompile)
  38. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  39. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  40. ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  41. ON_WM_PAINT()
  42. ON_WM_SETFOCUS()
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CWorkspaceBar 消息处理程序
  46. int CFileView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  47. {
  48. lpCreateStruct->style |= AFX_CBRS_REGULAR_TABS;
  49. lpCreateStruct->style |= AFX_CBRS_RESIZE;
  50. if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  51. return -1;
  52. CRect rectDummy;
  53. rectDummy.SetRectEmpty();
  54. // 创建视图:
  55. const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS;
  56. if (!m_wndFileView.Create(dwViewStyle, rectDummy, this, 4))
  57. {
  58. TRACE0("未能创建文件视图\n");
  59. return -1; // 未能创建
  60. }
  61. // 加载视图图像:
  62. m_FileViewImages.Create(IDB_FILE_VIEW, 16, 0, RGB(255, 0, 255));
  63. m_wndFileView.SetImageList(&m_FileViewImages, TVSIL_NORMAL);
  64. m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_EXPLORER);
  65. m_wndToolBar.LoadToolBar(IDR_EXPLORER, 0, 0, TRUE /* 已锁定*/);
  66. OnChangeVisualStyle();
  67. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
  68. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
  69. m_wndToolBar.SetOwner(this);
  70. // 所有命令将通过此控件路由,而不是通过主框架路由:
  71. m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
  72. // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据)
  73. FillFileView();
  74. AdjustLayout();
  75. return 0;
  76. }
  77. void CFileView::OnSize(UINT nType, int cx, int cy)
  78. {
  79. CDockablePane::OnSize(nType, cx, cy);
  80. AdjustLayout();
  81. }
  82. void CFileView::FillFileView()
  83. {
  84. HTREEITEM hRoot = m_wndFileView.InsertItem(_T("FakeApp 文件"), 0, 0);
  85. m_wndFileView.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);
  86. HTREEITEM hSrc = m_wndFileView.InsertItem(_T("FakeApp 源文件"), 0, 0, hRoot);
  87. m_wndFileView.InsertItem(_T("FakeApp.cpp"), 1, 1, hSrc);
  88. m_wndFileView.InsertItem(_T("FakeApp.rc"), 1, 1, hSrc);
  89. m_wndFileView.InsertItem(_T("FakeAppDoc.cpp"), 1, 1, hSrc);
  90. m_wndFileView.InsertItem(_T("FakeAppView.cpp"), 1, 1, hSrc);
  91. m_wndFileView.InsertItem(_T("MainFrm.cpp"), 1, 1, hSrc);
  92. m_wndFileView.InsertItem(_T("StdAfx.cpp"), 1, 1, hSrc);
  93. HTREEITEM hInc = m_wndFileView.InsertItem(_T("FakeApp 头文件"), 0, 0, hRoot);
  94. m_wndFileView.InsertItem(_T("FakeApp.h"), 2, 2, hInc);
  95. m_wndFileView.InsertItem(_T("FakeAppDoc.h"), 2, 2, hInc);
  96. m_wndFileView.InsertItem(_T("FakeAppView.h"), 2, 2, hInc);
  97. m_wndFileView.InsertItem(_T("Resource.h"), 2, 2, hInc);
  98. m_wndFileView.InsertItem(_T("MainFrm.h"), 2, 2, hInc);
  99. m_wndFileView.InsertItem(_T("StdAfx.h"), 2, 2, hInc);
  100. HTREEITEM hRes = m_wndFileView.InsertItem(_T("FakeApp 资源文件"), 0, 0, hRoot);
  101. m_wndFileView.InsertItem(_T("FakeApp.ico"), 2, 2, hRes);
  102. m_wndFileView.InsertItem(_T("FakeApp.rc2"), 2, 2, hRes);
  103. m_wndFileView.InsertItem(_T("FakeAppDoc.ico"), 2, 2, hRes);
  104. m_wndFileView.InsertItem(_T("FakeToolbar.bmp"), 2, 2, hRes);
  105. m_wndFileView.Expand(hRoot, TVE_EXPAND);
  106. m_wndFileView.Expand(hSrc, TVE_EXPAND);
  107. m_wndFileView.Expand(hInc, TVE_EXPAND);
  108. }
  109. void CFileView::OnContextMenu(CWnd* pWnd, CPoint point)
  110. {
  111. CTreeCtrl* pWndTree = (CTreeCtrl*) &m_wndFileView;
  112. ASSERT_VALID(pWndTree);
  113. if (pWnd != pWndTree)
  114. {
  115. CDockablePane::OnContextMenu(pWnd, point);
  116. return;
  117. }
  118. if (point != CPoint(-1, -1))
  119. {
  120. // 选择已单击的项:
  121. CPoint ptTree = point;
  122. pWndTree->ScreenToClient(&ptTree);
  123. UINT flags = 0;
  124. HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
  125. if (hTreeItem != NULL)
  126. {
  127. pWndTree->SelectItem(hTreeItem);
  128. }
  129. }
  130. pWndTree->SetFocus();
  131. theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EXPLORER, point.x, point.y, this, TRUE);
  132. }
  133. void CFileView::AdjustLayout()
  134. {
  135. if (GetSafeHwnd() == NULL)
  136. {
  137. return;
  138. }
  139. CRect rectClient;
  140. GetClientRect(rectClient);
  141. int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
  142. m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
  143. m_wndFileView.SetWindowPos(NULL, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
  144. }
  145. void CFileView::OnProperties()
  146. {
  147. AfxMessageBox(_T("属性...."));
  148. }
  149. void CFileView::OnFileOpen()
  150. {
  151. // TODO: 在此处添加命令处理程序代码
  152. }
  153. void CFileView::OnFileOpenWith()
  154. {
  155. // TODO: 在此处添加命令处理程序代码
  156. }
  157. void CFileView::OnDummyCompile()
  158. {
  159. // TODO: 在此处添加命令处理程序代码
  160. }
  161. void CFileView::OnEditCut()
  162. {
  163. // TODO: 在此处添加命令处理程序代码
  164. }
  165. void CFileView::OnEditCopy()
  166. {
  167. // TODO: 在此处添加命令处理程序代码
  168. }
  169. void CFileView::OnEditClear()
  170. {
  171. // TODO: 在此处添加命令处理程序代码
  172. }
  173. void CFileView::OnPaint()
  174. {
  175. CPaintDC dc(this); // 用于绘制的设备上下文
  176. CRect rectTree;
  177. m_wndFileView.GetWindowRect(rectTree);
  178. ScreenToClient(rectTree);
  179. rectTree.InflateRect(1, 1);
  180. dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
  181. }
  182. void CFileView::OnSetFocus(CWnd* pOldWnd)
  183. {
  184. CDockablePane::OnSetFocus(pOldWnd);
  185. m_wndFileView.SetFocus();
  186. }
  187. void CFileView::OnChangeVisualStyle()
  188. {
  189. m_wndToolBar.CleanUpLockedImages();
  190. m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_EXPLORER_24 : IDR_EXPLORER, 0, 0, TRUE /* 锁定 */);
  191. m_FileViewImages.DeleteImageList();
  192. UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_FILE_VIEW_24 : IDB_FILE_VIEW;
  193. CBitmap bmp;
  194. if (!bmp.LoadBitmap(uiBmpId))
  195. {
  196. TRACE(_T("无法加载位图: %x\n"), uiBmpId);
  197. ASSERT(FALSE);
  198. return;
  199. }
  200. BITMAP bmpObj;
  201. bmp.GetBitmap(&bmpObj);
  202. UINT nFlags = ILC_MASK;
  203. nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;
  204. m_FileViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
  205. m_FileViewImages.Add(&bmp, RGB(255, 0, 255));
  206. m_wndFileView.SetImageList(&m_FileViewImages, TVSIL_NORMAL);
  207. }