Creating a view on a dialog

Source: Internet
Author: User

 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:

    1. You shoshould hide the previous parent window.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.