ClassView.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "ClassView.h"
  14. #include "Resource.h"
  15. #include "IDE.h"
  16. class CClassViewMenuButton : public CMFCToolBarMenuButton
  17. {
  18. friend class CClassView;
  19. DECLARE_SERIAL(CClassViewMenuButton)
  20. public:
  21. CClassViewMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
  22. {
  23. }
  24. virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
  25. BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
  26. {
  27. pImages = CMFCToolBar::GetImages();
  28. CAfxDrawState ds;
  29. pImages->PrepareDrawImage(ds);
  30. CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);
  31. pImages->EndDrawImage(ds);
  32. }
  33. };
  34. IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1)
  35. //////////////////////////////////////////////////////////////////////
  36. // 构造/析构
  37. //////////////////////////////////////////////////////////////////////
  38. CClassView::CClassView() noexcept
  39. {
  40. m_nCurrSort = ID_SORTING_GROUPBYTYPE;
  41. }
  42. CClassView::~CClassView()
  43. {
  44. }
  45. BEGIN_MESSAGE_MAP(CClassView, CDockablePane)
  46. ON_WM_CREATE()
  47. ON_WM_SIZE()
  48. ON_WM_CONTEXTMENU()
  49. ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction)
  50. ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable)
  51. ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition)
  52. ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties)
  53. ON_COMMAND(ID_NEW_FOLDER, OnNewFolder)
  54. ON_WM_PAINT()
  55. ON_WM_SETFOCUS()
  56. ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort)
  57. ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort)
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CClassView 消息处理程序
  61. int CClassView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  62. {
  63. if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  64. return -1;
  65. CRect rectDummy;
  66. rectDummy.SetRectEmpty();
  67. // 创建视图:
  68. const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  69. if (!m_wndClassView.Create(dwViewStyle, rectDummy, this, 2))
  70. {
  71. TRACE0("未能创建类视图\n");
  72. return -1; // 未能创建
  73. }
  74. // 加载图像:
  75. m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_SORT);
  76. m_wndToolBar.LoadToolBar(IDR_SORT, 0, 0, TRUE /* 已锁定*/);
  77. OnChangeVisualStyle();
  78. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
  79. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
  80. m_wndToolBar.SetOwner(this);
  81. // 所有命令将通过此控件路由,而不是通过主框架路由:
  82. m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
  83. CMenu menuSort;
  84. menuSort.LoadMenu(IDR_POPUP_SORT);
  85. m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));
  86. CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
  87. if (pButton != nullptr)
  88. {
  89. pButton->m_bText = FALSE;
  90. pButton->m_bImage = TRUE;
  91. pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
  92. pButton->SetMessageWnd(this);
  93. }
  94. // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据)
  95. FillClassView();
  96. return 0;
  97. }
  98. void CClassView::OnSize(UINT nType, int cx, int cy)
  99. {
  100. CDockablePane::OnSize(nType, cx, cy);
  101. AdjustLayout();
  102. }
  103. void CClassView::FillClassView()
  104. {
  105. HTREEITEM hRoot = m_wndClassView.InsertItem(_T("FakeApp 类"), 0, 0);
  106. m_wndClassView.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);
  107. HTREEITEM hClass = m_wndClassView.InsertItem(_T("CFakeAboutDlg"), 1, 1, hRoot);
  108. m_wndClassView.InsertItem(_T("CFakeAboutDlg()"), 3, 3, hClass);
  109. m_wndClassView.Expand(hRoot, TVE_EXPAND);
  110. hClass = m_wndClassView.InsertItem(_T("CFakeApp"), 1, 1, hRoot);
  111. m_wndClassView.InsertItem(_T("CFakeApp()"), 3, 3, hClass);
  112. m_wndClassView.InsertItem(_T("InitInstance()"), 3, 3, hClass);
  113. m_wndClassView.InsertItem(_T("OnAppAbout()"), 3, 3, hClass);
  114. hClass = m_wndClassView.InsertItem(_T("CFakeAppDoc"), 1, 1, hRoot);
  115. m_wndClassView.InsertItem(_T("CFakeAppDoc()"), 4, 4, hClass);
  116. m_wndClassView.InsertItem(_T("~CFakeAppDoc()"), 3, 3, hClass);
  117. m_wndClassView.InsertItem(_T("OnNewDocument()"), 3, 3, hClass);
  118. hClass = m_wndClassView.InsertItem(_T("CFakeAppView"), 1, 1, hRoot);
  119. m_wndClassView.InsertItem(_T("CFakeAppView()"), 4, 4, hClass);
  120. m_wndClassView.InsertItem(_T("~CFakeAppView()"), 3, 3, hClass);
  121. m_wndClassView.InsertItem(_T("GetDocument()"), 3, 3, hClass);
  122. m_wndClassView.Expand(hClass, TVE_EXPAND);
  123. hClass = m_wndClassView.InsertItem(_T("CFakeAppFrame"), 1, 1, hRoot);
  124. m_wndClassView.InsertItem(_T("CFakeAppFrame()"), 3, 3, hClass);
  125. m_wndClassView.InsertItem(_T("~CFakeAppFrame()"), 3, 3, hClass);
  126. m_wndClassView.InsertItem(_T("m_wndMenuBar"), 6, 6, hClass);
  127. m_wndClassView.InsertItem(_T("m_wndToolBar"), 6, 6, hClass);
  128. m_wndClassView.InsertItem(_T("m_wndStatusBar"), 6, 6, hClass);
  129. hClass = m_wndClassView.InsertItem(_T("Globals"), 2, 2, hRoot);
  130. m_wndClassView.InsertItem(_T("theFakeApp"), 5, 5, hClass);
  131. m_wndClassView.Expand(hClass, TVE_EXPAND);
  132. }
  133. void CClassView::OnContextMenu(CWnd* pWnd, CPoint point)
  134. {
  135. CTreeCtrl* pWndTree = (CTreeCtrl*)&m_wndClassView;
  136. ASSERT_VALID(pWndTree);
  137. if (pWnd != pWndTree)
  138. {
  139. CDockablePane::OnContextMenu(pWnd, point);
  140. return;
  141. }
  142. if (point != CPoint(-1, -1))
  143. {
  144. // 选择已单击的项:
  145. CPoint ptTree = point;
  146. pWndTree->ScreenToClient(&ptTree);
  147. UINT flags = 0;
  148. HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
  149. if (hTreeItem != nullptr)
  150. {
  151. pWndTree->SelectItem(hTreeItem);
  152. }
  153. }
  154. pWndTree->SetFocus();
  155. CMenu menu;
  156. menu.LoadMenu(IDR_POPUP_SORT);
  157. CMenu* pSumMenu = menu.GetSubMenu(0);
  158. if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
  159. {
  160. CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;
  161. if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
  162. return;
  163. ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
  164. UpdateDialogControls(this, FALSE);
  165. }
  166. }
  167. void CClassView::AdjustLayout()
  168. {
  169. if (GetSafeHwnd() == nullptr)
  170. {
  171. return;
  172. }
  173. CRect rectClient;
  174. GetClientRect(rectClient);
  175. int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
  176. m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
  177. m_wndClassView.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
  178. }
  179. BOOL CClassView::PreTranslateMessage(MSG* pMsg)
  180. {
  181. return CDockablePane::PreTranslateMessage(pMsg);
  182. }
  183. void CClassView::OnSort(UINT id)
  184. {
  185. if (m_nCurrSort == id)
  186. {
  187. return;
  188. }
  189. m_nCurrSort = id;
  190. CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
  191. if (pButton != nullptr)
  192. {
  193. pButton->SetImage(GetCmdMgr()->GetCmdImage(id));
  194. m_wndToolBar.Invalidate();
  195. m_wndToolBar.UpdateWindow();
  196. }
  197. }
  198. void CClassView::OnUpdateSort(CCmdUI* pCmdUI)
  199. {
  200. pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort);
  201. }
  202. void CClassView::OnClassAddMemberFunction()
  203. {
  204. AfxMessageBox(_T("添加成员函数..."));
  205. }
  206. void CClassView::OnClassAddMemberVariable()
  207. {
  208. // TODO: 在此处添加命令处理程序代码
  209. }
  210. void CClassView::OnClassDefinition()
  211. {
  212. // TODO: 在此处添加命令处理程序代码
  213. }
  214. void CClassView::OnClassProperties()
  215. {
  216. // TODO: 在此处添加命令处理程序代码
  217. }
  218. void CClassView::OnNewFolder()
  219. {
  220. AfxMessageBox(_T("新建文件夹..."));
  221. }
  222. void CClassView::OnPaint()
  223. {
  224. CPaintDC dc(this); // 用于绘制的设备上下文
  225. CRect rectTree;
  226. m_wndClassView.GetWindowRect(rectTree);
  227. ScreenToClient(rectTree);
  228. rectTree.InflateRect(1, 1);
  229. dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
  230. }
  231. void CClassView::OnSetFocus(CWnd* pOldWnd)
  232. {
  233. CDockablePane::OnSetFocus(pOldWnd);
  234. m_wndClassView.SetFocus();
  235. }
  236. void CClassView::OnChangeVisualStyle()
  237. {
  238. m_ClassViewImages.DeleteImageList();
  239. UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;
  240. CBitmap bmp;
  241. if (!bmp.LoadBitmap(uiBmpId))
  242. {
  243. TRACE(_T("无法加载位图: %x\n"), uiBmpId);
  244. ASSERT(FALSE);
  245. return;
  246. }
  247. BITMAP bmpObj;
  248. bmp.GetBitmap(&bmpObj);
  249. UINT nFlags = ILC_MASK;
  250. nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;
  251. m_ClassViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
  252. m_ClassViewImages.Add(&bmp, RGB(255, 0, 0));
  253. m_wndClassView.SetImageList(&m_ClassViewImages, TVSIL_NORMAL);
  254. m_wndToolBar.CleanUpLockedImages();
  255. m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* 锁定*/);
  256. }