Create a view program without document classes using MFC

Source: Internet
Author: User
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;

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.