Add cscrollview and cformview dynamically in the cformview or dialog box
This Code creates a cscrollview based on the previously drawn control location in cformview.
You can also create cscrollview and cformview in cdialog.
Note:
If the following code is placed in cmainrightview: oncreate (lpcreatestruct), The getdlgitem () function fails to be called because the control has not been created yet!
Void cmainrightview: oninitialupdate ()
{
Cformview: oninitialupdate ();
// Todo: add your specialized code here and/or call the base class
// Obtain the target position control
Uint targetctrlid = idc_static_scrollview;
Cwnd * pwnd = This-> getdlgitem (targetctrlid );
Crect recttargetctrl;
Pwnd-> getwindowrect (recttargetctrl );
Pwnd-> destroywindow ();
This-> screentoclient (recttargetctrl );
// Create cscrollview dynamically at the target location
Cemapview * pemapview = (cemapview *) runtime_class (cemapview)-> Createobject ();
Pemapview-> Create (null, null, afx_ws_default_view, recttargetctrl, this, targetctrlid );
// The oninitialupdate function cannot be automatically called for a view created using createview. You need to manually call the oninitialupdate function or send the wm_initialupdate message.
Pemapview-> oninitialupdate ();
// Setscrollsizes () must be called; otherwise, an Assert error occurs during running. Of course, it can also be called in oninitialupdate () in the target view.
Pemapview-> setscrollsizes (mm_text, csize (recttargetctrl. Width ()-10, recttargetctrl. Height ()-10 ));
// Views created using createview are not automatically displayed and activated. manual operations are required.
Pemapview-> showwindow (sw_show );
}
Note: If you need to create cscrollview and cformview in cdialog, you need to create the following four methods in view overload and override. Otherwise, an Assert error occurs.
Afx_msg int onmouseactivate (cwnd * p0000topwnd, uint nhittest, uint message );
Afx_msg void ondestroy ();
Virtual void postncdestroy ();
Virtual void onactivateframe (uint nstate, cframewnd * pdeactivateframe );
Int cformviewprint: onmouseactivate (cwnd * p0000topwnd, uint nhittest, uint message)
{
// Todo: add your message handler code here and/or call default
Return cwnd: onmouseactivate (psf-topwnd, nhittest, message );
}
Void cformviewprint: ondestroy ()
{
Cwnd: ondestroy ();
// Todo: add your message handler code here
}
Void cformviewprint: postncdestroy ()
{
// Todo: add your specialized code here and/or call the base class
Cwnd: postncdestroy ();
}
Void cformviewprint: onactivateframe (uint nstate, cframewnd * pdeactivateframe)
{
// Todo: add your specialized code here and/or call the base class
Cwnd: onactivateframe (nstate, pdeactivateframe );
}
For the reason, refer
Difference between view and control (how to use the cview class in the dialog box)
The difference between the cview inheritance class and other window classes is that it is very important to operate the cdocument class and cframewnd class. Among them, operations involving the cdocument class, the validity is determined (m_pdocument! = NULL). When the cview class is initialized, m_pdocument = NULL does not affect the use of the cview class as a control. Operations involving the cframe class are as follows:
First place:Cview: ondestroy ().
Void cview: ondestroy ()
{
Cframewnd * pframe = getparentframe ();
If (pframe! = NULL & pframe-> getactiveview () = This)
Pframe-> setactiveview (null); // deactivate during death
Cwnd: ondestroy ();
}
Second place:Cview: onactivateframe ().
Void cview: onactivateframe (uint, cframewnd *)
{
}
This is actually empty.CviewOnlyCformviewClass inherits this virtual function
Void cformview: onactivateframe (uint nstate, cframewnd *)
{
If (nstate = wa_inactive)
Savefocuscontrol (); // save focus whenframe loses Activation
}
In fact, they do not need to be real.CframePointerCviewClass as a control.
Third place:Cview: onmouseactivate ().
Int cview: onmouseactivate (cwnd * p0000topwnd, uint nhittest, uint message)
{
Int nresult = cwnd: onmouseactivate (psf-topwnd, nhittest, message );
If (nresult = ma_noactivate | nresult = ma_noactivateandeat)
Return nresult; // frame does not want to activate
Cframewnd * pparentframe = getparentframe ();
If (pparentframe! = NULL)
{
// Eat it if this will cause Activation
Assert (pparentframe = p0000topwnd | p0000topwnd-> ischild (pparentframe ));
// Either re-activate the current view, or set this view to beactive
Cview * pview = pparentframe-> getactiveview ();
Hwnd hwndfocus =: getfocus ();
If (pview = This &&
M_hwnd! = Hwndfocus &&! : Ischild (m_hwnd, hwndfocus ))
{
// Re-activate this view
Onactivateview (true, this, this );
}
Else
{
// Activate this view
Pparentframe-> setactiveview (this );
}
}
Return nresult;
}
In additionCview: postncdestroy (), ImplementedCviewClass self-destruction, this is becauseCviewClass can be dynamically generated (Declare_dyncreate/implement_dyncreate).
// Self destruction
Void cview: postncdestroy ()
{
// Default for views is to allocate them on the heap
// The default post-cleanup is to 'delete this '.
// Never explicitly call 'delete' on a view
Delete this;
Basically, modifiedCviewIf you directly return the inheritance class, you can use it in the dialog box instead of calling the corresponding member functions of the base class.
The following example shows how to use the Wizard to generate a dialog box application. The dialog box class isCmydigalog.
SlaveChtmlviewClass inherits oneChtmlctrlClass, create the corresponding message processing function and modify the following:
// Chtmlctrl Message Processing Program
Int chtmlctrl: onmouseactivate (cwnd * p0000topwnd, uint nhittest, uintmessage)
{
// Todo:Add the message processing program code and/Or call the default value
Return cwnd: onmouseactivate (psf-topwnd, nhittest, message );
// Return chtmlview: onmouseactivate (psf-topwnd, nhittest, message );
}
Void chtmlctrl: ondestroy ()
{
// Chtmlview: ondestroy ();
// Todo:Add the message processing program code here
Cwnd: ondestroy ();
}
Void chtmlctrl: postncdestroy ()
{
// Todo:Add dedicated code and/Or call the base class
// Chtmlview: postncdestroy ();
}
Void chtmlctrl: onactivateframe (uint nstate, cframewnd * pdeactivateframe)
{
// Todo:Add dedicated code and/Or call the base class
// Chtmlview: onactivateframe (nstate, pdeactivateframe );
}
Add a member functionCreatefromctrl:
Public:
Bool createfromctrl (uint NID, cwnd * pparent );
This function createsChtmlctrlControls to facilitate layout during Dialog Box Design:
Bool chtmlctrl: createfromctrl (uint NID, cwnd * pparent)
{
If (! Pparent |! Pparent-> getsafehwnd ())
Return false;
Cwnd * pctrl = pparent-> getdlgitem (NID );
If (! Pctrl)
Return false;
Crect rcctrl;
Pctrl-> getwindowrect (rcctrl );
Pparent-> screentoclient (rcctrl );
Uint style =: getwindowlong (pctrl-> getsafehwnd (), gwl_style );
Pctrl-> destroywindow ();
Return create (null, null, style | ws_child | ws_visible, rcctrl, pparent, NID, null );
}
Note that the constructor and destructor of the default class inherited from cview are protected and must be modified to public.
Note that the constructor and destructor of the default class inherited from cview are protected and must be modified to public.
Public:
Chtmlctrl ();//Dynamically create protected constructors used
Virtual ~ Chtmlctrl ();
Then, insertStatic textControl,IDIsIdc_html.
In the header file of the dialog box, insert the include file:
# Include "htmlctrl. H"
AddChtmlctrlType member variables:
Chtmlctrl m_ctlhtml;
When the dialog box is initialized, create the chtmlctrl control:
Bool cmydialog: oninitdialog ()
{
Cdialog: oninitdialog ();
...
M_ctlhtml.createfromctrl (idc_html, this );
Return true;
}
Next, add a button to open it in the message response function of the button.HtmlFile:
Void cmydialog: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}