Embedding CFormView in CDockablePane

Source: Internet
Author: User

CDockablePane embedded CFormView and embedded CDialogEx slightly different, the difference is mainly reflected in the CFormView class itself and CDialogEx class, the cdockablepane level of the operation is identical.

A) Create a single document application;

b) Join the dialog resource, note that the dialog must have the child property, border set to None

A class derived from CFormView that can be associated with a dialog box resource. However, the dialog resource must have the [child] attribute selected in the property settings style, otherwise,
The code can be compiled, but the debug run will report an assertion error, the trace code, and the assertion in:

#ifdef _DEBUG

Dialog templatemust exist and be invisible with ws_child set
if (!_afxcheckdialogtemplate (M_lpsztemplatename, TRUE))
{
ASSERT (FALSE); Invalid dialog template name
PostNcDestroy (); Cleanup Ifcreate fails too soon
return FALSE;
}

#endif//_debug

CFormView is special, a parent form nested a subform, so an instance of a derived class of the CFormView class does not respond to the WM_CLOSE message, only the WM_DESTROY message. Also, to close the current view in code, you cannot directly: PostMessage (wm_close,0,0), but you must first get a pointer to the parent form and then send a WM_CLOSE message to the parent form, like this: GetParent ()-> PostMessage (wm_close,0,0) to achieve the goal. "Easy to understand MFC" Eighth Chapter 461 page Figure 8-1 clearly illustrates this situation, the view window is a child window of the CChildFrame window.

c) Create the class cformviewembeded for the dialog box, the base class is CFormView

D) overload the CREATE function of the cformviewembeded class to change the access permission to public (base class is protect) without adding extra code, in order to use the function in the program

e) Rewrite the onmouseactive message response function of the cformviewembeded class in order to prevent the program from crashing when the CDockablePane is in a suspended state (does not overload the inevitable crash!). )

Intcformviewembeded::onmouseactivate (cwnd* Pdesktopwnd, UINT nhittest, Uintmessage)

{

TODO: Add Message Handler code here

int nresult = 0;

Cframewnd*pparentframe = GetParentFrame ();

if (Pparentframe = = Pdesktopwnd)

{

When this is docked

Nresult=cformview::onmouseactivate (Pdesktopwnd, nhittest, message);

}

Else

{

When this isn't docked

Boolisminiframewnd =pdesktopwnd->iskindof (Runtime_class (CMiniFrameWnd));

Boolispaneframewnd =pdesktopwnd->iskindof (Runtime_class (CPaneFrameWnd));

Boolismultipaneframewnd =

Pdesktopwnd->iskindof (Runtime_class (CMultiPaneFrameWnd));

Pdesktopwnd is the frame window for CDockablePane

nresult= cwnd::onmouseactivate (Pdesktopwnd, nhittest, message);

}

return nresult;

}

f) Create a class derived from CDockablePane Cdockableformview

g) Because the constructor of the CFormView class has access to protect, the variable cannot be declared directly, so a member variable m_pformviewembeded of cdialogembeded* type is added for Cdockableformview. (another workaround is to declare CMainFrame as a friend Class);

h) Add in the constructor of the Cdockableformview class

m_pformviewembeded =

(cformviewembeded*) (Runtime_class (cformviewembeded))->createobject ();

i) OnCreate function for overloaded Cdockableformview

int Cdockableformview::oncreate (LPCREATESTRUCTLPCREATESTRUCT)

{

if (cdockablepane::oncreate (lpcreatestruct) = =-1)

return-1;

TODO: Add your dedicated code here

CRect rect;

GetClientRect (&rect);

M_pformviewembeded->create (null,null,ws_child| Ws_visible,rect,this,0,null);

return 0;

}

A) OnSize function for overloaded Cdockableformview

void Cdockableformview::onsize (UINT nType, intcx, int cy)

{

Cdockablepane::onsize (NType, CX, CY);

TODO: Add Message Handler code here

if (M_pformviewembeded->getsafehwnd ())

{

CRect rect;

GetClientRect (&rect);

M_pformviewembeded->setwindowpos (NULL,

Rect.left,rect.top,rect. Width (), Rect. Height (),

swp_noactivate| Swp_nozorder);

}

}

b) ondestory function for overloaded Cdockableformview

void Cdockableformview::ondestroy ()

{

Cdockablepane::ondestroy ();

TODO: Add Message Handler code here

M_pformviewembeded->destroywindow ();

}

c) Add the Cdockabledlg object to the Framework class M_dockdlg

D) Add the following code to the OnCreate function of the CMainFrame class, where 1002 of the CREATE function is the ID of the docking bar, and here is a random value, as long as it is not duplicated with other used resources and is actually applied to add an ID to the string table.

int CMainFrame::OnCreate (lpcreatestruct lpcreatestruct)

{

...

EnableDocking (Cbrs_align_any);

M_dockdlg.create ("Dockformview", This,crect (0,0,200,200), true,1002,

ws_child| ws_visible| ws_clipsiblings| ws_clipchildren| Cbrs_right| CBRS_FLOAT_MULTI);

M_dockdlg.enabledocking (Cbrs_align_any);

DockPane (&m_dockformview);

return 0;

}

E) Question: When will the M_dockformview be destroyed?

Embedding CFormView in CDockablePane

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.