现在想编一个小游戏,但又不想完全使用sdk,因而想单独使用主框架加视图类完成!
现在遇到的问题是无法单独使用CFormView的视图类!希望有不使用Doc/View的兄弟姐妹们指点一下。
我现在尝试如下:BOOL CStep6App::InitInstance()
{#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
m_pMainWnd = pMainFrame;

return TRUE;
}//然后在主窗体通过菜单显示不同窗体:
void CMainFrame::OnBegin() 
{
// TODO: Add your command handler code here
CRuntimeClass* prt = RUNTIME_CLASS(CStep6View); CStep6View *pBounceWnd = (CStep6View *)prt->CreateObject();; pBounceWnd->ShowWindow(SW_NORMAL);
pBounceWnd->UpdateWindow();

return;
}视图类的实现是采用向导生成的,运行就出现异常!如何修改呢?唉,感觉MFC灵活性实在太差,每个类基本不能单独使用,远远没有使用java或sdk方便!
如果动不动就要看源代码也实在太烦了!希望有人能提供范例!MSDN里面提供的例子其父类是CMDIChildWnd,我想改成CFormView!

解决方案 »

  1.   

    BOOL CDemoApp::InitInstance()
    {
    AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif // Change the registry key under which our settings are stored.
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    // To create the main window, this code creates a new frame window
    // object and then sets it as the application's main window object. CMainFrame* pFrame = new CMainFrame;
    m_pMainWnd = pFrame; // create and load the frame with its resources pFrame->LoadFrame(IDR_MAINFRAME);
    pFrame->SetIcon(LoadIcon(IDR_MAINFRAME),FALSE); // The one and only window has been initialized, so show and update it.
    pFrame->ShowWindow(SW_SHOW);
    pFrame->UpdateWindow(); return TRUE;
    }
    你的OnBegin是部队的,下面是运行视图的代码:
    bool DeleteView()
    { CDocument * pDoc= NULL;
    CCreateContext context;
    pDoc=this->GetActiveDocument();
    ASSERT(pDoc); context.m_pNewViewClass=RUNTIME_CLASS(CNetObserverView);
    context.m_pCurrentDoc=NULL;
    context.m_pNewDocTemplate=NULL;
    context.m_pLastView=NULL;
    context.m_pCurrentFrame=NULL;
    if(m_pSplt==NULL)
    {
    TRACE("delete  View is error!\n");
    return false; }

    pDoc->m_bAutoDelete=FALSE;   
    if (m_pSplt->m_hWnd)
    {
    m_pSplt->DestroyWindow();
    }
    delete m_pSplt;
    m_pSplt=NULL;
        pDoc->m_bAutoDelete=TRUE;
    //下面要创建父窗口的右侧视图
    m_wndSplitter.CreateView(0,1,context.m_pNewViewClass,CSize(100, 100), &context);
    CView * pNewView= (CView *)m_wndSplitter.GetPane(0,1);
    pNewView->OnInitialUpdate();
    SetActiveView(pNewView);
    m_wndSplitter.RecalcLayout();
    TRACE("delete  View is good!\n"); return true;
    }bool AddView(CRuntimeClass *pNewViewClass)
    {
    //if(m_pSplt!=NULL) return ;
    //添加View,用于服务列表视图
    CDocument * pDoc= NULL;
    CCreateContext context;
    pDoc=this->GetActiveDocument();
    ASSERT(pDoc);
    ASSERT(m_pSplt==NULL);
    //设备上下文
    // if(m_pSplt!=NULL) return false;
    context.m_pNewViewClass=pNewViewClass;
    context.m_pCurrentDoc=NULL;
    context.m_pNewDocTemplate=NULL;
    context.m_pLastView=NULL;
    context.m_pCurrentFrame=NULL; pDoc->m_bAutoDelete=FALSE;
    CView* pV = (CView *)m_wndSplitter.GetPane(0,1);
    ASSERT(pV);
    pV->DestroyWindow();
        pDoc->m_bAutoDelete=TRUE;
    m_pSplt = new CSplitterWnd;
    if (!m_pSplt->CreateStatic (&m_wndSplitter, 2,1,WS_CHILD|WS_VISIBLE,m_wndSplitter.IdFromRowCol(0,1)))
    {
    TRACE("Add View is error!\n");
    return false;
    }
    CView * pNewView;

    context.m_pNewViewClass=RUNTIME_CLASS(CNetObserverView);
    m_pSplt->CreateView(0,0,context.m_pNewViewClass,CSize(100, 100), &context);
    pNewView= (CView *)m_pSplt->GetPane(0,0);
    pNewView->OnInitialUpdate(); context.m_pNewViewClass=pNewViewClass;

    m_pSplt->CreateView(1,0,pNewViewClass,CSize(100, 100), &context);
    pNewView= (CView *)m_pSplt->GetPane(1,0);
    pNewView->OnInitialUpdate();
    SetActiveView(pNewView);
    m_pSplt->RecalcLayout();
    m_wndSplitter.RecalcLayout();
    RecalcLayout();
    TRACE("Add View is good!\n"); return true;
    }
    先DeleteView()再AddView*()
      

  2.   

    这有什么区别?还是使用了Doc/view的内部结构,没有实现类的独立性!
      

  3.   

    我想要的是独立出来,目前看来是不大可能了!看来还是sdk自由多了!
      

  4.   

    没有doc/view 又使用CFormView??那就直接用CDialog吧
      

  5.   

    可以但是不推荐这么做,需要对MFC的代码有深入了解,比用对话框麻烦多了