Creating a View on a Dialog
Posted
by Naama Goraly
on August 9th, 2001
-->
Environment: NT4 SP3
Creating a view on a child frame is a basic action when programming in MFC, but what happens when you want your view to be attached to a popup dialog? This article shows a simple way to make this possible.
The idea is to create the view in the usual way (through the document template, for example), and then have the dialog set as the parent. The previous parent--probably the child frame--should be stored and set back when the dialog is destroyed. Two more steps are needed:
- You should hide the previous parent window.
- You should override the OnMouseActivate method in your view in order to prevent an assertion in the base implementation
// ============================================= // In the creator window -- // ============================================= // when you want to show the view void CViewOnDlgView::OnShowdlgBtn() { // Add the desired document template CDocTemplate* pDocTemplate = NULL; pDocTemplate = new CMultiDocTemplate( IDR_INNERVIEW, RUNTIME_CLASS(CrInnerDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CrInnerView)); AfxGetApp()->AddDocTemplate(pDocTemplate); // Create an instance of the desired document template CString processName; bool bIsFound = false; CWinApp* pApp = AfxGetApp(); POSITION curTemplatePos = pApp->GetFirstDocTemplatePosition(); while(curTemplatePos) { pDocTemplate = pApp->GetNextDocTemplate(curTemplatePos); if (pDocTemplate) { pDocTemplate->GetDocString(processName , CDocTemplate::docName); if( processName == "InnerView") { bIsFound = true; break; } else processName = ""; } } if (bIsFound) pDoc = pDocTemplate->OpenDocumentFile(NULL); // send a message to the view to move itself to // the dialog POSITION pos = pDoc->GetFirstViewPosition(); while (pos) { CView* pView = pDoc->GetNextView(pos); pView->SendMessage(WM_SHOW_ON_DLG, 0, 0); } } // ============================================= // In the view you want to put on the dialog // ============================================= int CrInnerView::ShowOnDlg(WPARAM wParam, LPARAM lParam) { // hiding the previous parent GetParent()->ShowWindow(SW_HIDE); // creating the bearer dialog CrPopupDlg dlg; dlg.SetView(this); dlg.DoModal(); return 0; } // this method needs to be overridden to prevent // an assertion int CrInnerView::OnMouseActivate( CWnd* pDesktopWnd, UINT nHitTest, UINT message ) { return MA_ACTIVATE; } // ============================================= // In the bearer dialog // ============================================= BOOL CrPopupDlg::OnInitDialog() { if (m_pView && ::IsWindow(m_pView->m_hWnd)) { // set this as the view's parent and save // the old parent m_pPrevParent = m_pView->SetParent(this); // resize the view to the measures of the dlg CRect rect; GetClientRect(rect); m_pView->MoveWindow(rect); } return TRUE; } void CrPopupDlg::SetView(CView* pView) { // saves a pointer to the view we want to attach // to this dlg m_pView = pView; } void CrPopupDlg::OnDestroy() { CDialog::OnDestroy(); // set the previous parent window back as active // parent and destroy both windows if (m_pPrevParent && ::IsWindow(m_pPrevParent->m_hWnd)) { m_pView->SetParent(m_pPrevParent); m_pView->GetParent()->DestroyWindow(); } }
Downloads
Download demo project - 36 KbDownload source - 24 Kb

Comments
Clicking on inner view in release mode causes the program to crash
Posted by rsnprabha on 07/20/2005 12:45amClicking on view in release mode causes the program to crash. Looks like the program crahses in OnMouseActivate. any solutions?
ReplyEasy way.. and works.
Posted by Legacy on 01/20/2004 12:00amOriginally posted by: Rajesh
ReplyDialog
Posted by Legacy on 09/26/2003 12:00amOriginally posted by: Saleem Anwar
Replyhelp me...
Posted by Legacy on 10/24/2002 12:00amOriginally posted by: mihee
From Dialog , I made Split Window and composed of view separately.
I have a trouble when i tried to call the above dialog from mainframe..
In the debug mode, i solved it by overloading the OnMouseActivate event.
But it was crashed in the Release mode.
So, If you have a solution about what I said, please send me your answer.
Thank your for your consideration.
ReplyAn easier way to create a view in Dialog(just 11 lines)
Posted by Legacy on 08/08/2002 12:00amOriginally posted by: Z Mohamed Mustafa
Replya similar idea
Posted by Legacy on 06/17/2002 12:00amOriginally posted by: Thomas Schuster
Just look up this ...
Replyhttp://www.codeproject.com/useritems/View_in_Dialog.asp
CView in a Dialog-Based works fine. How do I print it now?
Posted by Legacy on 04/03/2002 12:00amOriginally posted by: Hugo
I've followed Jean-Claude`s example using a CHtmlView and it works fine, but I need to print the view now. How do I print the web pages displayed in my view?
ReplyThanks in advance
Hugo
CRichEditCtrl in a Dialog based app
Posted by Legacy on 03/13/2002 12:00amOriginally posted by: John
Has any had tried inserting RichEditCtrl in Dialog based
Replyapp? I would like to display .rtf content in a small
RichEditCtrl window in the app dialog.
ADDING CONTROLS OF PARENT DIALOG TO A ANOTHER DIALOG AT RUN TIME ON SWITCHING
Posted by Legacy on 01/04/2002 12:00amOriginally posted by: sameer
is it possible to add the controls of a parent dialog to another dialog on switching at run time so that the controls behave as part of both the dialogs.
pls help
sameer
ReplyCreating view in a dialog based application
Posted by Legacy on 11/17/2001 12:00amOriginally posted by: Rejeesh.T.S
The sample you have given is a doc/vew application.
ReplyIs it possible to create a view in a Dialog based application?
Loading, Please Wait ...