FileView.cpp 7.0 KB

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