123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
- // (“Fluent UI”)。该示例仅供参考,
- // 用以补充《Microsoft 基础类参考》和
- // MFC C++ 库软件随附的相关电子文档。
- // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
- // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
- // https://go.microsoft.com/fwlink/?LinkId=238214.
- //
- // 版权所有(C) Microsoft Corporation
- // 保留所有权利。
- #include "stdafx.h"
- #include "MainFrm.h"
- #include "ClassView.h"
- #include "Resource.h"
- #include "IDE.h"
- class CClassViewMenuButton : public CMFCToolBarMenuButton
- {
- friend class CClassView;
- DECLARE_SERIAL(CClassViewMenuButton)
- public:
- CClassViewMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
- {
- }
- virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
- BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
- {
- pImages = CMFCToolBar::GetImages();
- CAfxDrawState ds;
- pImages->PrepareDrawImage(ds);
- CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);
- pImages->EndDrawImage(ds);
- }
- };
- IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1)
- //////////////////////////////////////////////////////////////////////
- // 构造/析构
- //////////////////////////////////////////////////////////////////////
- CClassView::CClassView() noexcept
- {
- m_nCurrSort = ID_SORTING_GROUPBYTYPE;
- }
- CClassView::~CClassView()
- {
- }
- BEGIN_MESSAGE_MAP(CClassView, CDockablePane)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_CONTEXTMENU()
- ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction)
- ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable)
- ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition)
- ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties)
- ON_COMMAND(ID_NEW_FOLDER, OnNewFolder)
- ON_WM_PAINT()
- ON_WM_SETFOCUS()
- ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort)
- ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CClassView 消息处理程序
- int CClassView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDockablePane::OnCreate(lpCreateStruct) == -1)
- return -1;
- CRect rectDummy;
- rectDummy.SetRectEmpty();
- // 创建视图:
- const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
- if (!m_wndClassView.Create(dwViewStyle, rectDummy, this, 2))
- {
- TRACE0("未能创建类视图\n");
- return -1; // 未能创建
- }
- // 加载图像:
- m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_SORT);
- m_wndToolBar.LoadToolBar(IDR_SORT, 0, 0, TRUE /* 已锁定*/);
- OnChangeVisualStyle();
- m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
- m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
- m_wndToolBar.SetOwner(this);
- // 所有命令将通过此控件路由,而不是通过主框架路由:
- m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
- CMenu menuSort;
- menuSort.LoadMenu(IDR_POPUP_SORT);
- m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));
- CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
- if (pButton != nullptr)
- {
- pButton->m_bText = FALSE;
- pButton->m_bImage = TRUE;
- pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
- pButton->SetMessageWnd(this);
- }
- // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据)
- FillClassView();
- return 0;
- }
- void CClassView::OnSize(UINT nType, int cx, int cy)
- {
- CDockablePane::OnSize(nType, cx, cy);
- AdjustLayout();
- }
- void CClassView::FillClassView()
- {
- HTREEITEM hRoot = m_wndClassView.InsertItem(_T("FakeApp 类"), 0, 0);
- m_wndClassView.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);
- HTREEITEM hClass = m_wndClassView.InsertItem(_T("CFakeAboutDlg"), 1, 1, hRoot);
- m_wndClassView.InsertItem(_T("CFakeAboutDlg()"), 3, 3, hClass);
- m_wndClassView.Expand(hRoot, TVE_EXPAND);
- hClass = m_wndClassView.InsertItem(_T("CFakeApp"), 1, 1, hRoot);
- m_wndClassView.InsertItem(_T("CFakeApp()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("InitInstance()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("OnAppAbout()"), 3, 3, hClass);
- hClass = m_wndClassView.InsertItem(_T("CFakeAppDoc"), 1, 1, hRoot);
- m_wndClassView.InsertItem(_T("CFakeAppDoc()"), 4, 4, hClass);
- m_wndClassView.InsertItem(_T("~CFakeAppDoc()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("OnNewDocument()"), 3, 3, hClass);
- hClass = m_wndClassView.InsertItem(_T("CFakeAppView"), 1, 1, hRoot);
- m_wndClassView.InsertItem(_T("CFakeAppView()"), 4, 4, hClass);
- m_wndClassView.InsertItem(_T("~CFakeAppView()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("GetDocument()"), 3, 3, hClass);
- m_wndClassView.Expand(hClass, TVE_EXPAND);
- hClass = m_wndClassView.InsertItem(_T("CFakeAppFrame"), 1, 1, hRoot);
- m_wndClassView.InsertItem(_T("CFakeAppFrame()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("~CFakeAppFrame()"), 3, 3, hClass);
- m_wndClassView.InsertItem(_T("m_wndMenuBar"), 6, 6, hClass);
- m_wndClassView.InsertItem(_T("m_wndToolBar"), 6, 6, hClass);
- m_wndClassView.InsertItem(_T("m_wndStatusBar"), 6, 6, hClass);
- hClass = m_wndClassView.InsertItem(_T("Globals"), 2, 2, hRoot);
- m_wndClassView.InsertItem(_T("theFakeApp"), 5, 5, hClass);
- m_wndClassView.Expand(hClass, TVE_EXPAND);
- }
- void CClassView::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- CTreeCtrl* pWndTree = (CTreeCtrl*)&m_wndClassView;
- ASSERT_VALID(pWndTree);
- if (pWnd != pWndTree)
- {
- CDockablePane::OnContextMenu(pWnd, point);
- return;
- }
- if (point != CPoint(-1, -1))
- {
- // 选择已单击的项:
- CPoint ptTree = point;
- pWndTree->ScreenToClient(&ptTree);
- UINT flags = 0;
- HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
- if (hTreeItem != nullptr)
- {
- pWndTree->SelectItem(hTreeItem);
- }
- }
- pWndTree->SetFocus();
- CMenu menu;
- menu.LoadMenu(IDR_POPUP_SORT);
- CMenu* pSumMenu = menu.GetSubMenu(0);
- if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
- {
- CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;
- if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
- return;
- ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
- UpdateDialogControls(this, FALSE);
- }
- }
- void CClassView::AdjustLayout()
- {
- if (GetSafeHwnd() == nullptr)
- {
- return;
- }
- CRect rectClient;
- GetClientRect(rectClient);
- int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
- m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
- m_wndClassView.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
- }
- BOOL CClassView::PreTranslateMessage(MSG* pMsg)
- {
- return CDockablePane::PreTranslateMessage(pMsg);
- }
- void CClassView::OnSort(UINT id)
- {
- if (m_nCurrSort == id)
- {
- return;
- }
- m_nCurrSort = id;
- CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
- if (pButton != nullptr)
- {
- pButton->SetImage(GetCmdMgr()->GetCmdImage(id));
- m_wndToolBar.Invalidate();
- m_wndToolBar.UpdateWindow();
- }
- }
- void CClassView::OnUpdateSort(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort);
- }
- void CClassView::OnClassAddMemberFunction()
- {
- AfxMessageBox(_T("添加成员函数..."));
- }
- void CClassView::OnClassAddMemberVariable()
- {
- // TODO: 在此处添加命令处理程序代码
- }
- void CClassView::OnClassDefinition()
- {
- // TODO: 在此处添加命令处理程序代码
- }
- void CClassView::OnClassProperties()
- {
- // TODO: 在此处添加命令处理程序代码
- }
- void CClassView::OnNewFolder()
- {
- AfxMessageBox(_T("新建文件夹..."));
- }
- void CClassView::OnPaint()
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- CRect rectTree;
- m_wndClassView.GetWindowRect(rectTree);
- ScreenToClient(rectTree);
- rectTree.InflateRect(1, 1);
- dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
- }
- void CClassView::OnSetFocus(CWnd* pOldWnd)
- {
- CDockablePane::OnSetFocus(pOldWnd);
- m_wndClassView.SetFocus();
- }
- void CClassView::OnChangeVisualStyle()
- {
- m_ClassViewImages.DeleteImageList();
- UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;
- CBitmap bmp;
- if (!bmp.LoadBitmap(uiBmpId))
- {
- TRACE(_T("无法加载位图: %x\n"), uiBmpId);
- ASSERT(FALSE);
- return;
- }
- BITMAP bmpObj;
- bmp.GetBitmap(&bmpObj);
- UINT nFlags = ILC_MASK;
- nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;
- m_ClassViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
- m_ClassViewImages.Add(&bmp, RGB(255, 0, 0));
- m_wndClassView.SetImageList(&m_ClassViewImages, TVSIL_NORMAL);
- m_wndToolBar.CleanUpLockedImages();
- m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* 锁定*/);
- }
|