当基础的框架搭好以后,我想为其添加一个左侧导航栏,过程如下:
在框架类的头文件添加一个导航栏参数:
CMFCOutlookBar m_navigation; |
为了完善功能,在导航栏里面我添加了一个CTreeCtrl控件
并且创建一个函数创建导航栏,并关联CTreeCtrl控件,函数原型如下:
bool CreateNavigationBar(CMFCOutlookBar& bar, UINT uiID, int nInitialWidth, CTreeCtrl& treectrl); |
uiID为CMFCOutlookBar的ID,
nInitialWidth为导航栏的宽度,函数的实现如下
bool CBridgeWindow::CreateNavigationBar(CMFCOutlookBar& bar, UINT uiID, int nInitialWidth, CTreeCtrl& treectrl){ //创建导航栏 bar.Create("节点",this,CRect(0, 0, 250, 32000),uiID,WS_CHILD | WS_VISIBLE | CBRS_LEFT); CMFCOutlookBarTabCtrl* pOutlookBar = (CMFCOutlookBarTabCtrl*)m_navigation.GetUnderlyingWindow(); if (pOutlookBar == NULL) { ASSERT(FALSE); return FALSE; } //pOutlookBar->EnableInPlaceEdit(TRUE);//标签能够被编辑 DWORD dwStyle = AFX_CBRS_FLOAT | AFX_CBRS_AUTOHIDE | AFX_CBRS_RESIZE; CRect rectDummy(0, 0, 0, 0); const DWORD dwTreeStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS; treectrl.Create(dwTreeStyle,rectDummy,&bar,ID_TREELIST); TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = "河北"; tvInsert.item.lParam=1; HTREEITEM hBridge = treectrl.InsertItem(&tvInsert); tvInsert.item.pszText = "山东"; tvInsert.item.lParam=2; TVINSERTSTRUCT tNode; //tNode.hInsertAfter=hBridge; tNode.hParent=hBridge; tNode.item.mask=TVIF_TEXT; tNode.item.pszText="石家庄"; tNode.item.lParam=1; treectrl.InsertItem(&tNode); hBridge=treectrl.InsertItem(&tvInsert); treectrl.InsertItem(TVIF_TEXT,"济南", 0, 0, 0, 0, 0, hBridge, NULL); pOutlookBar->AddControl(&treectrl, "节点列表", 2, TRUE, dwStyle); return false;} |
调用代码
int CBridgeWindow::OnCreate(LPCREATESTRUCT lpCreateStruct){ if (CFrameWndEx::OnCreate(lpCreateStruct) == -1) return -1; CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue); CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(false); RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE); m_wndRibbonBar.Create(this); m_wndRibbonBar.LoadFromResource(IDR_RIBBON); // 启用 Visual Studio 2005 样式停靠窗口行为// CDockingManager::SetDockingMode(DT_SMART); // 启用 Visual Studio 2005 样式停靠窗口自动隐藏行为// EnableAutoHidePanes(CBRS_ALIGN_ANY); // 导航窗格将创建在左侧,因此将暂时禁用左侧的停靠:// EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM | CBRS_ALIGN_RIGHT); //创建左侧导航栏 CreateNavigationBar(m_navigation,ID_NAVIGATION,250,m_treectrl); return 0;} |
当然,要在resource.h中添加资源ID:ID_NAVIGATION,
ID_TREELIST,编译运行即可
源文: http://blog.chinaunix.net/uid-24585655-id-2125546.html