MFC Program The most important thing is the document view framework. Do not create documents now? Although the online materials are transferred
Write another memo.
# Include <Afxwin. h>// MFCCore Components and standard components
# Include <Afxext. h>// MFCExtension
// ================================================ =View class =================================================== =====
ClassCmyview:PublicCview
{
//The constructor is declared as protected, which means that it can only be created inside the framework.
Protected:
Cmyview ()
{
}
//Can be dynamically created
Declare_dyncreate (cmyview)
//Declare message ing
Declare_message_map ()
//Customize background Erasure
Afx_msg bool onerasebkgnd (CDC * PDC)
{
Rect;
This-> Getclientrect (& rect );
Colorref color = RGB (1, 200,200,220 );
Cbrush brush (color );
PDC-> fillrect (& rect, & brush );
ReturnTrue;
}
Public:
//This function must be implemented. This is a pure virtual function of the cview class, And cview is an abstract class.
Virtual VoidOndraw (CDC * PDC)
{
PDC-> setbkmode (transparent );
PDC-> textoutw (100,100, cstring ("This is my view"));
}
//The Destructor is public.
~ Cmyview ()
{
}
};
//Dynamic creation capability
Implement_dyncreate (cmyview, cview)
//Message ing
Begin_message_map (cmyview, cview)
On_wm_erasebkgnd ()
End_message_map ()
// ================================================ =Main framework window class ================================================= ======
ClassCmainframe:PublicCframewnd
{
Protected:
//Supports dynamic creation
Declare_dyncreate (cmainframe)
//Message ing supported
Declare_message_map ()
//View
Cmyview * m_pmainview;
// OncreateMethod, called only when the window is created, caused by the wm_create message
Afx_msgIntOncreate (maid)
{
//First, complete the method of the base class
If(Cframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
//To create a view, you must declare a context view structure.
Ccreatecontext CCX;
CCX. m_pnewviewclass = runtime_class (cmyview );
CCX. m_pcurrentframe =This;
CCX. m_pcurrentdoc = NULL;
CCX. m_pnewdoctemplate = NULL;
CCX. m_plastview = NULL;
//Create a view using struct
M_pmainview = dynamic_downcast (cmyview,This-> Createview (& CCX ));
//If creation fails
If(! M_pmainview)
{
Trace0 ("Creation of view failed");
}
//Relayout the main framework
Recalclayout ();
//Display View
M_pmainview-> showwindow (sw_show );
M_pmainview-> updatewindow ();
Return0;
}
Public:
Cmainframe ()
{
M_pmainview = NULL;
}
~ Cmainframe ()
{
}
};
//Dynamic creation capability
Implement_dyncreate (cmainframe, cframewnd)
//Message ing
Begin_message_map (cmainframe, cframewnd)
On_wm_create ()
End_message_map ()
// ================================================ =Main Application class ================================================= ========
ClassCmyapp:PublicCwinapp
{
Public:
Cmyapp ()
{
}
VirtualBool initinstance ()
{
Cwinapp: initinstance ();
Cmainframe * pframe =NewCmainframe ();
Pframe-> Create (null, (lpctstr) _ T ("Hello"));
This-> M_pmainwnd = pframe;
This-> M_pmainwnd-> showwindow (sw_show );
This-> M_pmainwnd-> updatewindow ();
ReturnTrue;
}
};
Cmyapp theapp;