#include "pch.h" #include "framework.h" #include "Resource.h" #include "MainFrm.h" #include "NavView.h" #include "MTerm1.h" #define IDC_NAVTREE1 1046 class CClassViewMenuButton : public CMFCToolBarMenuButton { friend class CNavView; 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) ////////////////////////////////////////////////////////////////////// // 构造/析构 ////////////////////////////////////////////////////////////////////// CNavView::CNavView() noexcept { m_nCurrSort = ID_SORTING_GROUPBYTYPE; } CNavView::~CNavView() { } BEGIN_MESSAGE_MAP(CNavView, 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) ON_NOTIFY(NM_DBLCLK, IDC_NAVTREE1, OnNMDblclkNavTree1) ON_NOTIFY(NM_CLICK, IDC_NAVTREE1, OnNMClkNavTree1) ON_WM_TIMER() END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CNavView 消息处理程序 int CNavView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDockablePane::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty(); // 创建视图: const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_LINESATROOT | TVS_HASLINES | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; // if (!m_NavViewTree.Create(dwViewStyle, rectDummy, this, IDC_NAVTREE1)) { TRACE0("未能创建导航栏视图\n"); return -1; // 未能创建 } m_NavViewTree.SetExtendedStyle(TVS_EX_DOUBLEBUFFER, TVS_EX_DOUBLEBUFFER); // 加载图像: 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); } // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据) SetTimer(0, 100, NULL); return 0; } void CNavView::OnSize(UINT nType, int cx, int cy) { CDockablePane::OnSize(nType, cx, cy); AdjustLayout(); } void CNavView::FillClassView() { HTREEITEM hRoot1 = m_NavViewTree.InsertItem(_T("连接1 Via COM3 230400 8-N-1 KLinkV1.00"), 0, 0); m_NavViewTree.SetItemState(hRoot1, TVIS_BOLD, TVIS_BOLD); HTREEITEM hItem; HTREEITEM hClass1 = m_NavViewTree.InsertItem(_T("KL10-E16D-N1 1/1 Run, Good"), 5, 5, hRoot1); m_NavViewTree.SetItemData(hClass1, 10); hItem = m_NavViewTree.InsertItem(_T("设备信息"), 3, 3, hClass1); m_NavViewTree.SetItemData(hItem, 11); hItem = m_NavViewTree.InsertItem(_T("工作状态,系统参数"), 3, 3, hClass1); m_NavViewTree.SetItemData(hItem, 12); HTREEITEM hPort2 = m_NavViewTree.InsertItem(_T("端口2 485 KBus 本机地址(0)/5 Run "), 1, 1, hClass1); m_NavViewTree.SetItemData(hPort2, 13); hItem = m_NavViewTree.InsertItem(_T("1 KL10-E08D-N1 Run Good"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, 131); hItem = m_NavViewTree.InsertItem(_T("2 KL10-E08D-N1 Run bad"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, 132); hItem = m_NavViewTree.InsertItem(_T("3 KL10-E08D-N1 Run Good"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, 133); hItem = m_NavViewTree.InsertItem(_T("4 KL10-E08D-N1 Run Good"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, 134); hItem = m_NavViewTree.InsertItem(_T("5 KL10-E08D-N1 Run bad"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, 135); HTREEITEM hPort3 = m_NavViewTree.InsertItem(_T("端口3 WireLess "), 1, 1, hClass1); m_NavViewTree.SetItemData(hPort3, 14); m_NavViewTree.SetBkColor(COLORREF(0xf0f0f0f0) ); m_NavViewTree.Expand(hRoot1, TVE_EXPAND); HTREEITEM hClass2 = m_NavViewTree.InsertItem(_T("CFakeAboutDlg"), 1, 1, hRoot1); m_NavViewTree.SetItemData(hClass2, 20); hItem = m_NavViewTree.InsertItem(_T("CFakeAboutDlg()"), 3, 3, hClass2); m_NavViewTree.SetItemData(hItem, 21); HTREEITEM hClass3 = m_NavViewTree.InsertItem(_T("CFakeAppDoc"), 1, 1, hRoot1); m_NavViewTree.SetItemData(hClass3, 30); hItem = m_NavViewTree.InsertItem(_T("CFakeAppDoc()"), 4, 4, hClass3); m_NavViewTree.SetItemData(hItem, 31); hItem = m_NavViewTree.InsertItem(_T("~CFakeAppDoc()"), 3, 3, hClass3); m_NavViewTree.SetItemData(hItem, 32); hItem = m_NavViewTree.InsertItem(_T("OnNewDocument()"), 3, 3, hClass3); m_NavViewTree.SetItemData(hItem, 33); HTREEITEM hClass4 = m_NavViewTree.InsertItem(_T("CFakeAppView"), 1, 1, hRoot1); m_NavViewTree.SetItemData(hClass4, 40); hItem = m_NavViewTree.InsertItem(_T("CFakeAppView()"), 4, 4, hClass4); m_NavViewTree.SetItemData(hItem, 41); hItem = m_NavViewTree.InsertItem(_T("~CFakeAppView()"), 3, 3, hClass4); m_NavViewTree.SetItemData(hItem, 42); hItem = m_NavViewTree.InsertItem(_T("GetDocument()"), 3, 3, hClass4); m_NavViewTree.SetItemData(hItem, 43); m_NavViewTree.Expand(hClass4, TVE_EXPAND); HTREEITEM hClass5 = m_NavViewTree.InsertItem(_T("Globals"), 2, 2, hRoot1); m_NavViewTree.SetItemData(hClass5, 50); hItem = m_NavViewTree.InsertItem(_T("theFakeApp"), 5, 5, hClass5); m_NavViewTree.SetItemData(hItem, 51); m_NavViewTree.Expand(hClass5, TVE_EXPAND); HTREEITEM hRoot2 = m_NavViewTree.InsertItem(_T("连接状态2"), 0, 0); m_NavViewTree.SetItemState(hRoot2, TVIS_BOLD, TVIS_BOLD); } void CNavView::OnContextMenu(CWnd* pWnd, CPoint point) { CTreeCtrl* pWndTree = (CTreeCtrl*)&m_NavViewTree; 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 CNavView::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_NavViewTree.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER); } BOOL CNavView::PreTranslateMessage(MSG* pMsg) { return CDockablePane::PreTranslateMessage(pMsg); } void CNavView::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 CNavView::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (nIDEvent == 0) { KillTimer(0); DelayInit(); SetTimer(1, 200, NULL); } else if (nIDEvent == 1) { UpdateDisplay(); } else if (nIDEvent == 2) { } else if (nIDEvent == 3) { } CDockablePane::OnTimer(nIDEvent); } int CNavView::DelayInit() { // TODO: 在此处添加实现代码. FillClassView(); return 0; } int CNavView::UpdateDisplay() { // TODO: 在此处添加实现代码. // m_NavViewTree.DeleteAllItems(); // FillClassView(); CString s1; HTREEITEM hroot1 = m_NavViewTree.GetRootItem(); HTREEITEM hItem1 = m_NavViewTree.GetChildItem(hroot1); nDisplayCount++; s1.Format(_T("KL10-E16D-N1 1/1 Run, Good %d"), nDisplayCount); m_NavViewTree.SetItemText(hItem1, s1); return 0; } void CNavView::OnUpdateSort(CCmdUI* pCmdUI) { pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort); } void CNavView::OnClassAddMemberFunction() { AfxMessageBox(_T("添加成员函数...")); } void CNavView::OnClassAddMemberVariable() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnClassDefinition() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnClassProperties() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnNewFolder() { AfxMessageBox(_T("新建文件夹...")); } void CNavView::OnPaint() { CPaintDC dc(this); // 用于绘制的设备上下文 CRect rectTree; m_NavViewTree.GetWindowRect(rectTree); ScreenToClient(rectTree); rectTree.InflateRect(1, 1); dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW)); } void CNavView::OnSetFocus(CWnd* pOldWnd) { CDockablePane::OnSetFocus(pOldWnd); m_NavViewTree.SetFocus(); } void CNavView::OnChangeVisualStyle() { m_NavViewImages.DeleteImageList(); UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW; // uiBmpId = IDB_NAV_VIEW1; 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_NavViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0); m_NavViewImages.Add(&bmp, RGB(255, 0, 0)); m_NavViewTree.SetImageList(&m_NavViewImages, TVSIL_NORMAL); m_wndToolBar.CleanUpLockedImages(); m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* 锁定*/); } void CNavView::OnNMClkNavTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); // HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem; // HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem(); //以GetItemText()函数为例: // CString s2 = m_NavViewTree.GetItemText(hTreeItem); //AfxMessageBox(_T("You have selected ") + s1); // CString s1; // DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem); // s1.Format(_T("Form Clk action %d, id %d oldid %d data %d x %d y %d text %s "), // pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2); // SysLog(s1); *pResult = 0; } void CNavView::OnNMDblclkNavTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem(); //以GetItemText()函数为例: CString s2 = m_NavViewTree.GetItemText(hTreeItem); //AfxMessageBox(_T("You have selected ") + s1); CString s1; DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem); s1.Format(_T("Form dbclk action %d, id %d oldid %d data %d x %d y %d text %s "), pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2); SysLog(s1); *pResult = 0; }