Win32 框架文档视图(3)
2011-04-03 16:34
190 查看
5.19 如何实现滚动视图
其功能类似于WPF的ScrollViewer,具备滚动条void CDemoView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); //滚动视图的尺寸 CSize sizeTotal; sizeTotal.cx = 800; sizeTotal.cy = 600; //滚动条的页尺寸 CSize sizePage; sizePage.cx = 50; sizePage.cy = 50; //滚动条的行尺寸 CSize sizeLine; sizeLine.cx = 10; sizeLine.cy = 10; //设置滚动视图尺寸 SetScrollSizes(MM_TEXT, sizeTotal, sizePage, sizeLine); }
5.20 如何使用列表视图
CListViewvoid CDemoView::InitList() { //获得列表控件的指针 CListCtrl* pList = &GetListCtrl(); //设置列表视图的报表显示方式 pList->ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT); //设置列表视图显示网格线和整行选中 pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); //在列表视图中插入列 for (int n = 0; n < 4; n++) { CString strColumnHeading = _T(""); strColumnHeading.Format(_T("Column %d"), n); pList->InsertColumn(n, strColumnHeading, LVCFMT_LEFT, 100); } //在列表视图中插入行 for (int m = 0; m < 10; m++) { CString strItem = _T(""); strItem.Format("Item %d", m); pList->InsertItem(m, strItem); for (int n = 1; n < 4; n++) { CString strText = _T(""); strText.Format(_T("SubItem %d %d"), m, n); pList->SetItemText(m, n, strText); } } }
5.21 如何使用树视图
CTreeViewvoid CDemoView::InitTree() { //获得数控件 CTreeCtrl* pTree = &GetTreeCtrl(); //为树添加连线和按钮 pTree->ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS); CString strText = _T(""); //在树中插入项 HTREEITEM hRoot = pTree->InsertItem(_T("Root")); for (int i = 0; i < 4; i++) { strText.Format(_T("Item %d"), i); //在树中插入项 HTREEITEM hParent = pTree->InsertItem(strText, hRoot); for(int j = 0; j < 5; j++) { strText.Format(_T("SubItem %d %d"), i, j); //在树中插入项 pTree->InsertItem(strText, hParent); } //展开树项 pTree->Expand(hParent, TVE_EXPAND); } //展开树项 pTree->Expand(hRoot, TVE_EXPAND); //选择树项 pTree->Select(hRoot, TVGN_CARET); }
5.22 如何实现HTML视图
CHtmlViewvoid CDemoView::OnNavigate() { CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd(); CDialogBar* pDialogBar = &(pMainFrame->m_wndDlgBar); CString strAddress = _T(""); pDialogBar->GetDlgItemText(IDC_ADDRESS, strAddress); //浏览 Navigate2(strAddress); Invalidate(FALSE); } void CDemoView::OnGoBack() { //后退 GoBack(); } void CDemoView::OnGoForward() { //前进 GoForward(); } void CDemoView::OnStop() { //停止 Stop(); } void CDemoView::OnRefresh() { //刷新 Refresh(); }
5.23 如何在视图中获得文档指针
GetDocument方法void CDemoView::OnTest() { //获得文档指针 CDemoDoc* pDoc = GetDocument(); if (pDoc == NULL) { return; } CRect rect; GetClientRect(rect); CString strText = _T(""); strText.Format(_T("在视图中获得文档指针:0x%08X"), pDoc); CDC* pDC = GetDC(); pDC->SetTextAlign(TA_CENTER); pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText); }
5.24 如何在框架中获得当前视图指针
GetActiveView方法void CMainFrame::OnTest() { //获得当前子框架 CMDIChildWnd* pChildFrame = (CMDIChildWnd*)GetActiveFrame(); if (pChildFrame == NULL) { return; } //获得当前视图 CDemoView* pView = (CDemoView*)pChildFrame->GetActiveView(); if (pView == NULL) { return; } CRect rect; pView->GetClientRect(rect); CString strText = _T(""); strText.Format(_T("在框架中获得当前视图指针:0x%08X"), pView); CDC* pDC = pView->GetDC(); pDC->SetTextAlign(TA_CENTER); pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText); }
5.25 如何在框架中获得当前文档指针
GetActiveDocument方法void CMainFrame::OnTest() { //获得当前子框架 CMDIChildWnd* pChildFrame = (CMDIChildWnd*)GetActiveFrame(); if (pChildFrame == NULL) { return; } //获得当前视图 CDemoView* pView = (CDemoView*)pChildFrame->GetActiveView(); if (pView == NULL) { return; } //获得当前文档 CDemoDoc* pDoc = (CDemoDoc*)pChildFrame->GetActiveDocument(); if (pDoc == NULL) { return; } CRect rect; pView->GetClientRect(rect); CString strText = _T(""); strText.Format(_T("在框架中获得当前文档指针:0x%08X"), pDoc); CDC* pDC = pView->GetDC(); pDC->SetTextAlign(TA_CENTER); pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText); }
5.26 如何新建或打开文档
OpenDocumentFile方法void CDemoApp::OnTest1() { //新建文档 m_pDocTemplate->OpenDocumentFile(NULL); } void CDemoApp::OnTest2() { CString strPathName = _T(""); //文件对话框 CString strFilter = _T("所有文件(*.*)|*.*||"); CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter); dlg.m_ofn.lpstrTitle = _T("浏览文件"); if(dlg.DoModal() == IDOK) { strPathName = dlg.GetPathName(); } //打开文档 m_pDocTemplate->OpenDocumentFile(strPathName); }
5.27 如何创建新的框架窗口
CreateNewFrame方法void CMainFrame::OnTest() { CDemoApp* pApp = (CDemoApp*)AfxGetApp(); if (pApp == NULL) { return; } CMultiDocTemplate* pDocTemplate = pApp->m_pDocTemplate; CDocument* pDoc = pApp->m_pDoc; //创建新框架窗口 CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(pDoc, NULL); if (pFrame == NULL) { return; } //初始化框架窗口 pDocTemplate->InitialUpdateFrame(pFrame, pDoc); }
5.28 如何实现单文档-多视图结构
//创建文档模板1 CMultiDocTemplate* pDocTemplate1; pDocTemplate1 = new CMultiDocTemplate( IDR_DEMOTYPE, RUNTIME_CLASS(CDemoDoc), RUNTIME_CLASS(CChildFrame), RUNTIME_CLASS(CDemoView1)); AddDocTemplate(pDocTemplate1); //创建文档模板2 CMultiDocTemplate* pDocTemplate2; pDocTemplate2 = new CMultiDocTemplate( IDR_DEMOTYPE, RUNTIME_CLASS(CDemoDoc), RUNTIME_CLASS(CChildFrame), RUNTIME_CLASS(CDemoView2)); AddDocTemplate(pDocTemplate2); CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame; //文档模板1新建一个文档 CDemoDoc* pDoc = (CDemoDoc*)pDocTemplate1->OpenDocumentFile(NULL); if (pDoc == NULL) { return FALSE; } //文档模板2创建与文档相关联的框架窗口 CFrameWnd* pFrame = pDocTemplate2->CreateNewFrame(pDoc, NULL); if (pFrame == NULL) { return FALSE; } //文档模板2初始化框架窗口 pDocTemplate2->InitialUpdateFrame(pFrame, pDoc); pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); //水平排列窗口 pMainFrame->MDITile(MDITILE_HORIZONTAL);
5.29 如何在文档模板中遍历文档
GetFirstDocPositionGetNextDoc
void CDemoApp::OnTest() { int n = 0; //获得第一个文档的位置 POSITION pos1 = m_pDocTemplate->GetFirstDocPosition(); while(pos1 != NULL) { n++; //获得下一个文档 CDocument* pDoc = m_pDocTemplate->GetNextDoc(pos1); //更新视图 pDoc->UpdateAllViews(NULL, n); } }
5.30 如何在应用程序中遍历文档模板
同上类似void CDemoApp::OnTest() { int n = 0; //获得第一个文档模板的位置 POSITION pos1 = GetFirstDocTemplatePosition(); while(pos1 != NULL) { n++; //获得下一个文档模板 CDocTemplate* pDocTemplate = GetNextDocTemplate(pos1); if (pDocTemplate == NULL) { continue; } //获得第一个文档的位置 POSITION pos2 = pDocTemplate->GetFirstDocPosition(); while(pos2 != NULL) { //获得下一个文档 CDocument* pDoc = pDocTemplate->GetNextDoc(pos2); //更新视图 pDoc->UpdateAllViews(NULL, n); } } }
5.31 如何在文档中遍历视图
也同上比较相似,就是名字发生了变化void CDemoDoc::OnTest() { int n = 0; //获得第一个视图的位置 POSITION pos = GetFirstViewPosition(); while(pos != NULL) { n++; //获得下一个视图 CView* pView = GetNextView(pos); if (pView == NULL) { continue; } CRect rect; pView->GetClientRect(rect); CString strText = _T(""); strText.Format(_T("视图%d"), n); CDC* pDC = pView->GetDC(); pDC->SetTextAlign(TA_CENTER); pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText); } }
相关文章推荐
- Win32 框架文档视图(1)
- Win32 框架文档视图(2)
- MFC 框架文档视图关系
- vc++6.0中文档、视图与框架的互相调用
- MFC中获取各种类(文档、视图、框架、应用程序)指针的方法 .
- 详解MFC框架窗口、文档和视图
- MFC中文档视图框架和文档模板之间的关系
- 在基于对话框的程序中使用文档视图的框架,并将其用于打印
- (转)文档doc、视图view、框架frame相互之间的访问
- MFC中获取各种类(文档、视图、框架、应用程序)指针的方法
- 文档视图框架操作总结
- 框架窗口、文档和视图
- 获取主框架、子框架、文档、视图的指针
- VS2010/MFC编程入门之三十九(文档、视图和框架:概述)
- VS2010/MFC编程入门之四十一(文档、视图和框架:分割窗口)
- VS2010/MFC编程入门之四十(文档、视图和框架:各对象之间的关系)
- [MFC小笔记]框架/文档/视图之间的调用关系
- 文档、视图、框架窗口、文档模板之间的相互关系
- MFC多文档框架视图页面添加IE内核浏览器
- TN25 文档,视图和框架之创建