User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

    Creating a View in Dialog (An easy way).



    Environment developed: VC6 Win 2K]

    To create a view you normally follow the Microsoft's Document template model. (i.e) Single document template or multi doc template. You can pass three runtime classes to the constructors:

    CSingleDocTemplate( UINT nIDResource,
                        CRuntimeClass* pDocClass, 
                        CRuntimeClass* pFrameClass,
                        CRuntimeClass* pViewClass );
    

    OR

    CMultiDocTemplate( UINT nIDResource, 
                       CRuntimeClass* pDocClass,
                       CRuntimeClass* pFrameClass,
                       CRuntimeClass* pViewClass );
    

    When the document template is added, frame (child for MDI , main frame for SDI), document, and a view is created dynamically and added to the application's document template list.

    But some times you may need to create a view without using the default document template classes. For instance, if you are to create a view in dialog, you can not use the regular way of document template. In those cases you can use the following method:

    BOOL CVwindlgDlg::OnInitDialog()
    {
    
         ...
    
      CCreateContext pContext;
      /**
      * Note:CDialig derived pointer is converted to
      * CWnd pointer (a common base class for CDialog and CFrameWnd).
      * Thus casting it back to CFrameWnd is also easy.
      */
      CWnd* pFrameWnd = this;
      
      pContext.m_pCurrentDoc = new CMyDocument;
      pContext.m_pNewViewClass = RUNTIME_CLASS(CMyVw);
      CMyVw *pView =
         (CMyVw *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
      ASSERT(pView);
      pView->ShowWindow(SW_NORMAL);
      /**
      * After a view is created, resize that to 
      * have the same size as the dialog.
      */
      CRect rectWindow;
      GetWindowRect(rectWindow);
      /**
      * Leave a little space for border and title...
      */
      rectWindow.right += 15;
      rectWindow.top   -= 10;
      pView->MoveWindow(rectWindow);
      CString str(AfxGetApp()->m_lpCmdLine);
      /**
      * Note: "CMyVw" is a CHTMLView derived class to add some 
      *       spice to the view, I have provided a HTML page 
      *       to which it navigates when the dialog is up.
      */
      char strPath[255];
      ::GetCurrentDirectory(255,(LPSTR)(LPCSTR)strPath);
      strcat(strPath,"\\defaultpage.html");
      pView->Navigate(strPath);
    
             ....
    
      return TRUE;  // return TRUE  unless you set the 
                    // focus to a control
    }

    Downloads

    Download demo project - 6 KB
    Download source - 38 KB

    IT Offers


    Top Authors