• MFC中手动创建出视图


    在MDI程序中,新建和打开菜单都是系统自带的,有些时候并不能通过ON_FILE_NEW来显示出视图,某种类型的视图往往可能只显示一个。

    那么撇开系统自带的ON_FILE_NEW命令,我们自己写一个。
     
    在程序启动时,我们不想新建出一个空的视图,只要大的框架就行。
    在app的InitInstance函数中,将下面几行注释掉
     // 分析标准外壳命令、DDE、打开文件操作的命令行
    //CCommandLineInfo cmdInfo;
    //ParseCommandLine(cmdInfo);


    // 调度在命令行中指定的命令。如果
    // 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
    //if (!ProcessShellCommand(cmdInfo))
    // return FALSE;
    创建主框架的代码是:
     // 创建主 MDI 框架窗口
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
    {
    delete pMainFrame;
    return FALSE;
    }
    m_pMainWnd = pMainFrame;
    pMainFrame->ShowWindow(SW_SHOW);
    pMainFrame->UpdateWindow();
    为了演示创建视图,我们可以在菜单中添加一个菜单项,并在MainFrame中处理,当然在WinApp中也可以。代码很简单
    void CMainFrame::OnTestCreate()
    {
    // TODO: 在此添加命令处理程序代码
    CDocTemplate* pDocTemplate = ((CxxApp*)AfxGetApp())->m_pDocTemplate;
    ASSERT_VALID(pDocTemplate);
    pDocTemplate->OpenDocumentFile(NULL);

    }
    我翻过mfc里面的源码,主要处理是:
    CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
    BOOL bMakeVisible)
    {
    CDocument* pDocument = CreateNewDocument();
    if (pDocument == NULL)
    {
    TRACE(traceAppMsg, 0, "CDocTemplate::CreateNewDocument returned NULL.\n");
    AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
    return NULL;
    }
    ASSERT_VALID(pDocument);

    BOOL bAutoDelete = pDocument->m_bAutoDelete;
    pDocument->m_bAutoDelete = FALSE; // don't destroy if something goes wrong
    CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);
    pDocument->m_bAutoDelete = bAutoDelete;
    if (pFrame == NULL)
    {
    AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
    delete pDocument; // explicit delete on error
    return NULL;
    }
    ASSERT_VALID(pFrame);

    if (lpszPathName == NULL)
    {
    // create a new document - with default document name
    SetDefaultTitle(pDocument);

    // avoid creating temporary compound file when starting up invisible
    if (!bMakeVisible)
    pDocument->m_bEmbedded = TRUE;

    if (!pDocument->OnNewDocument())
    {
    // user has be alerted to what failed in OnNewDocument
    TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE.\n");
    pFrame->DestroyWindow();
    return NULL;
    }

    // it worked, now bump untitled count
    m_nUntitledCount++;
    }
    else
    {
    // open an existing document
    CWaitCursor wait;
    if (!pDocument->OnOpenDocument(lpszPathName))
    {
    // user has be alerted to what failed in OnOpenDocument
    TRACE(traceAppMsg, 0, "CDocument::OnOpenDocument returned FALSE.\n");
    pFrame->DestroyWindow();
    return NULL;
    }
    pDocument->SetPathName(lpszPathName);
    }

    InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
    return pDocument;
    }
    如果有些功能不是很适合,也可以参照该函数来修改。
  • 相关阅读:
    给你的博客园加个面板娘!
    idea实现简单热部署
    idea 上传svn忽略文件
    谈一谈AOP面向切面编程
    做一个自定义注解
    使用aop切面编写日志模块
    数据结构之链表
    数据结构之队列
    数据结构之栈
    数据结构之线性表
  • 原文地址:https://www.cnblogs.com/zhangyonghugo/p/2305318.html
Copyright © 2020-2023  润新知