Creating a View in Dialog (An easy way). | CodeGuru

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 ); […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 19, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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: “CMyVwis 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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.