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


Comments

  • problems on windows vista

    Posted by rbpkirow on 11/07/2008 03: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

    Reply
  • How to get a pointer to the CCustomView view?

    Posted by Mike Pliam on 09/18/2008 05:07pm

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

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

    Posted by Mike Pliam on 03/02/2007 03: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 ?

    Reply
  • how to add CDocument class to a Dialog

    Posted by Legacy on 02/10/2004 12: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

    Reply
  • change of e-mail address

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

    Originally posted by: Mohamed Mustafa Z

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

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

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

    Originally posted by: Tony

    Hello,

    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!!!

    Tony

    Reply
  • debug assertion failed!

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

    Originally posted by: ilheung

    how can i work this out?
    it says

    viewcore.cpp
    line: 249
    line: 1173

    urgent !!!

    Reply
  • Win XP Style controls

    Posted by Legacy on 10/03/2003 12: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??

    Reply
  • Urgent Help Needed for printing

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

    Originally posted by: Md Azghar Hussain

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

    Reply
  • Forget about it

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

    Originally posted by: Castor

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

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds