在MDI程序中实现类似WPS2000的文件切换标签 作者:山西大同 张聪
下载源代码 用过WPS2000的朋友,肯定对其文件切换功能有很深的印象。当打开多个文件时,他可以使我们快速的切换到指定的文件。本文将详细地说明如何在自己的MDI程序中加入这样一个标签。 图一是本文例子程序运行画面。 图一 例子程序运行画面 开始之前,我们先对WPS2000中的文件切换标签做简单分析,这是一个Tab标签,该标签具有以下功能:
Style: Child Border: Thin 去掉标题条和系统菜单选项; 字体:宋体,字号:10
选中按钮选项。//这使的Tab标签呈现按钮风格 选中Hot Track选项。//这使得鼠标移上时文字变蓝
void CMyTab::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code hereint idx = GetCurSel();TC_ITEM ti;ti.mask = TCIF_PARAM;GetItem(idx, &ti);CView * pView = (CView *) ti.lParam;((CMDIFrameWnd *)AfxGetMainWnd())->MDIActivate((pView->GetParent())->GetParent());*pResult = 0;*pResult = 0;}
void CMyTab::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default int idx = GetCurSel(); TC_ITEM ti; ti.mask = TCIF_PARAM; GetItem(idx, &ti); CView * pView = (CView *) ti.lParam; ((CMDIFrameWnd *)AfxGetMainWnd())->MDIActivate((pView->GetParent())->GetParent()); // maximize or restore MDIChild window based on its current state BOOL bMaximize=FALSE; CWnd* pActiveWnd=((CMDIFrameWnd *)AfxGetMainWnd())->MDIGetActive(&bMaximize); if(bMaximize)((CMDIFrameWnd *)AfxGetMainWnd())->MDIRestore(pActiveWnd); else((CMDIFrameWnd *)AfxGetMainWnd())->MDIMaximize(pActiveWnd);}
protected: CDialogBar m_wndTabBar;public:CMyTab m_MyTab;
ON_COMMAND_EX(ID_VIEW_TAB_BAR, OnBarCheck) ON_UPDATE_COMMAND_UI(ID_VIEW_TAB_BAR, OnUpdateControlBarMenu)
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){ //向导生成代码略去 //创建TAB条 if (!m_wndTabBar.Create(this, IDD_TAB_DLG_BAR,CBRS_TOP | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,ID_VIEW_TAB_BAR)) //ID_VIEW_TAB_BAR是菜单命令ID,用于显示或隐藏对话框条 {TRACE0("Failed to create dialog bar m_wndDialogbar\n");return -1;// fail to create } //m_wndTabBar.EnableDocking(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT); //EnableDocking(CBRS_ALIGN_ANY); //DockControlBar(&m_wndTabBar); //将m_MyTab与控件IDC_TAB绑定 m_MyTab.SubclassDlgItem(IDC_TAB, &m_wndTabBar); return 0;}
void CMainFrame::OnSize(UINT nType, int cx, int cy) { CMDIFrameWnd::OnSize(nType, cx, cy); // TODO: Add your message handler code here CRect rcClient; GetClientRect(&rcClient); int left,top,right,bottom; left=rcClient.left; top=rcClient.top; right=rcClient.right; bottom=rcClient.bottom; m_wndTabBar.MoveWindow(left,top+24,right-left,24,TRUE); CRect rcTab; m_wndTabBar.GetClientRect(&rcTab); CTabCtrl *pMyTab=(CTabCtrl *)(m_wndTabBar.GetDlgItem(IDC_TAB)); pMyTab->MoveWindow(rcTab,TRUE);}
public: CViewManager m_ViewManager;
void CTabMDIDemoView::OnInitialUpdate() { CView::OnInitialUpdate(); //取得文档标题及视指针并加入到视管理器数组及tab控件中 CMDIDialogbarDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CString cs=pDoc->GetTitle(); ((CMainFrame*)AfxGetMainWnd())->m_ViewManager.AddView(cs,this);}
CTabMDIDemoView::~CTabMDIDemoView(){ ((CMainFrame*)AfxGetMainWnd())->m_ViewManager.RemoveView(this);}
void CTabMDIDemoView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) {// TODO: Add your specialized code here and/or call the base class((CMainFrame*)AfxGetMainWnd())->m_ViewManager.OnActivateView(bActivate, this);CView::OnActivateView(bActivate, pActivateView, pDeactiveView);}