Creating a view on a dialog
(Continued)
-->
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
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 -- shocould be stored and set back when the dialog is destroyed. Two
More steps are needed:
- You shoshould hide the previous parent window.
- You shoshould 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
// 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, 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 * p1_topwnd,
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 KB
Download source-24 KB