Creating a View in Dialog (An easy way).
Posted
by Z Mohamed Mustafa
on February 19th, 2002
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 KBDownload source - 38 KB

Comments
problems on windows vista
Posted by rbpkirow on 11/07/2008 03:20amHello. 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
ReplyHow to get a pointer to the CCustomView view?
Posted by Mike Pliam on 09/18/2008 05:07pmCan draw using old pDC-> but not with GDI+ . Why ?
Posted by Mike Pliam on 03/02/2007 03:08pmThank 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 ?
Replyhow to add CDocument class to a Dialog
Posted by Legacy on 02/10/2004 12:00amOriginally 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
Replychange of e-mail address
Posted by Legacy on 11/05/2003 12:00amOriginally posted by: Mohamed Mustafa Z
my e-mail address is changed & plz use this for any further correspondence.
ReplyGreat! Would like an opinion, please...
Posted by Legacy on 10/23/2003 12:00amOriginally 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
Replydebug assertion failed!
Posted by Legacy on 10/08/2003 12:00amOriginally posted by: ilheung
how can i work this out?
it says
viewcore.cpp
line: 249
line: 1173
urgent !!!
ReplyWin XP Style controls
Posted by Legacy on 10/03/2003 12:00amOriginally 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??
ReplyUrgent Help Needed for printing
Posted by Legacy on 08/26/2003 12:00amOriginally posted by: Md Azghar Hussain
ReplyForget about it
Posted by Legacy on 08/03/2003 12:00amOriginally posted by: Castor
In release mode this doesn't work! Kernel errors etc.
Reply
Loading, Please Wait ...