Creating a View in Dialog (An easy way).


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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


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);
  * After a view is created, resize that to 
  * have the same size as the dialog.
  CRect rectWindow;
  * Leave a little space for border and title...
  rectWindow.right += 15;
  rectWindow.top   -= 10;
  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];


  return TRUE;  // return TRUE  unless you set the 
                // focus to a control


Download demo project - 6 KB
Download source - 38 KB


  • problems on windows vista

    Posted by rbpkirow on 11/07/2008 11:20am

    Hello. I'm trying to use this application in Windows Vista, but I have a problem with this. I download the demo project, and I have not problems on any windows (XP or Vista) I download the source code and build it with Visual 6 on XP, and I have a problem on Vista (not on XP) Vista said to me that the program doesn't run. Could you tell me how I must compile it? Is there some extra library that I am not including? thanks a lot rbpkirow

  • How to get a pointer to the CCustomView view?

    Posted by Mike Pliam on 09/19/2008 12:07am

    CCustomView *pView = (CCustomView *) ((CFrameWnd*)pFrameWnd)->GetActiveView(); 
    doesnt work and crashes.  Suggestions appreciated.

  • Can draw using old pDC-> but not with GDI+ . Why ?

    Posted by Mike Pliam on 03/02/2007 11:08pm

    Thank you for this wonderful bit of code. It is the most straightforward approach to create view in a dialog that I could find after days of searching in disappointment. You are a bit of a genius, I think. However, I am disappointed that I seem to be unable to draw using GDI+. I can draw with the old device context functions, but I had hoped to use GDI+ because of it's rich capabilities. GDI+ stuff all compiles and runs OK, but nothing shows up on the View. Anyone have any ideas ?

  • how to add CDocument class to a Dialog

    Posted by Legacy on 02/10/2004 08:00am

    Originally posted by: lilukshi silva

    could you please tell me the way to add the CDocument class to a dialog in order to store the data inserted in the controls on the dialog

  • change of e-mail address

    Posted by Legacy on 11/05/2003 08:00am

    Originally posted by: Mohamed Mustafa Z

    my e-mail address is changed & plz use this for any further correspondence.

  • Great! Would like an opinion, please...

    Posted by Legacy on 10/23/2003 07:00am

    Originally posted by: Tony


    This technique had helped me alot in my application. I have a pretty extensive application, but I am needing to NOT allow a user to right-click and get a context menu.

    I have a solution almost working right, but I would love to hear your thoughts on how you would go about doing this?

    Any help would be greatly appreciated!!!


  • debug assertion failed!

    Posted by Legacy on 10/08/2003 07:00am

    Originally posted by: ilheung

    how can i work this out?
    it says

    line: 249
    line: 1173

    urgent !!!

  • Win XP Style controls

    Posted by Legacy on 10/03/2003 07:00am

    Originally posted by: Geno Carman

    I notice the sample does NOT have WinXP style controls on the web page.

    My app uses all WinXP style controls, but those controls in my CHtmlView derived view do not !!!

    Any ideas on how to correct this??

  • Urgent Help Needed for printing

    Posted by Legacy on 08/26/2003 07:00am

    Originally posted by: Md Azghar Hussain

    I want to add printing support to this application, Can u help me out of this.

  • Forget about it

    Posted by Legacy on 08/03/2003 07:00am

    Originally posted by: Castor

    In release mode this doesn't work! Kernel errors etc.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date