zoukankan      html  css  js  c++  java
  • Win32 框架文档视图(3)

    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);
    }
    

    image

    5.20 如何使用列表视图

    CListView

    void 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 如何使用树视图

    CTreeView

    void 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视图

    CHtmlView

    void 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 如何在文档模板中遍历文档

    1. GetFirstDocPosition
    2. GetNextDoc
    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);
        }
    }
    
  • 相关阅读:
    PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
    PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
    PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***...
    PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
    PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)...
    PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
    R语言实现金融数据的时间序列分析及建模
    ES6 | class类的基本语法总结
    less使用总结
    umi 的项目中如何修改 favicon
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2004474.html
Copyright © 2011-2022 走看看