VC ++ technical insider note next day MFC Application Framework

Source: Internet
Author: User
Tags textout

MFC is a C ++ Microsoft Windows API
? The Application Generated by MFC uses a standardized structure.
? The applications generated by MFC are short and run fast.
? The VC ++ tool reduces the complexity of coding. Of course, many codes are done by it.
? The MFC Library application framework has rich functions.
All of the above are advantages of the MFC library. Although it has many advantages, I personally think that we cannot learn it blindly. To learn it well, you must first master C ++, this is beyond doubt. At the beginning, you may find it very rewarding and interesting, but it is very difficult to improve it without the C ++ Foundation. Therefore, the webmaster suggests that you have to first learn this before learning it!
The application framework is a collection of databases.
Let's take a look at an example to see how powerful MFC is! You only need to add one line of code, or even one line without a few mouse clicks to create a Windows program. If you don't believe it, try it:
1. Open VC ++ 6, select new from the menu, and name the project "MyApp ".
2. Select the MFC Appwizard [EXE] option. The default step is step 1.
3. In Class View, double-click the ondraw () member function of the cmyappview class to view the following content in the C ++ compiler.

Void cmyappview: ondraw (CDC * PDC)
{
Cmyappdoc * pdoc = getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native data here
}
Add a line of code at // todo: Add draw code for native data here.
Void cmyappview: ondraw (CDC * PDC)
{
Cmyappdoc * pdoc = getdocument ();
Assert_valid (pdoc );
PDC-> textout (10, 10, "May VC be your best friend to learn VC online! "); // Add a row
// Todo: Add draw code for native data here
}
That's easy. Compile and run. Have you seen it? This program has all the features of a Windows program, such as menus, toolbar, status bar, maximize, close, and even a dialog box, print preview ..... all. This is an application dynamically created by Appwizard through MFC. From this example, we can see how convenient it is to use VC/MFC to design Windows programs.

Let's take a look at the example in the book to better understand the application framework.
1. Create a Win32 application first.
2. Select project-> Add to project-> files to create a file named MyApp. h and MyApp. cpp respectively.
3. Add the Code: (it is best to break the code into the compiler. Do not use Ctrl + C/Ctrl + V)
//************************************** *********
// MyApp. h
//

Class cmyapp: Public cwinapp
{
Public:
Virtual bool initinstance ();
};

Class cmyframe: Public cframewnd
{
Public:
Cmyframe ();
Protected:
Afx_msg void onlbuttondown (uint nflags, cpoint point );
Afx_msg void onpaint ();
Declare_message_map ()
};

//************************************** ***************
// MyApp. cpp
//

# Include "afxwin. H"
# Include "MyApp. H"
Cmyapp theapp; // create a cmyapp object
Bool cmyapp: initinstance ()
{
M_pmainwnd = new cmyframe ();
M_pmainwnd-> showwindow (m_ncmdshow );
M_pmainwnd-> updatewindow ();
Return true;
}

Begin_message_map (cmyframe, cframewnd)
On_wm_lbuttondown ()
On_wm_paint ()
End_message_map ()

Cmyframe: cmyframe (){
Create (null, "MyApp application ");
}
Void cmyframe: onlbuttondown (uint nflags, cpoint point)
{
Trace ("entering cmyframe: onlbuttondown-% lx, % d, % d/N ",
(Long) nflags, point. X, point. y );
}

Void cmyframe: onpaint ()
{
Cpaintdc DC (this );
DC. textout (0, 0, "Hello world! ");
}

4. Compile and run. An error is returned. Why? No support for MFC has been added. On the project setting option general properties page, select "use MFC in a static library"
5. Press Ctrl + F5. How is it easy?
Let's take a look at some elements in this program.
① Winmain function: Windows always requires every application to have a winmain function. You cannot see it because it is hidden inside the application framework.
② Cmyapp class: the objects of the cmyapp Class represent an application. The cwinapp base class determines most of its behaviors.
③ Application startup: Windows calls the winmain function when running the application, and winmain searches for theapp, The Global Object of the application.
④ Cmyapp: initinstance member function: After theapp is found, the overloaded virtual function initinstance is automatically called to construct and display the main window.
⑤ Cwinapp: Run member function: winmain calls the run function immediately after initinstance is called. It is hidden in the window in the base class that is responsible for transmitting the messages of the application to the ing.
⑥ Cmyframe class: the objects of this Class represent the main window of the application. Its constructor calls the create function of the base class cframewnd to create a specific window structure.
7. cmyframe: onlbuttondown function: demonstrate message processing mechanism. When you press the mouse key, this event is mapped to the onlbuttondown function of cmyframe, if you choose F5 for compilation and running, you can see the following information displayed by the trace macro In the debugging window.
Entering cmyframe: onlbuttondown-1,309,119.
Entering cmyframe: onlbuttondown-1,408,221.
⑧ Cmyframe: onpaint function: This function is called every time the application re-draws a window. "Hello World!" is displayed! "It is placed here because every time the window changes, ensure" Hello world! "Displayed, you can try to set the statement:
Cpaintdc DC (this );
DC. textout (0, 0, "Hello world! ");
Write it out, for example
Void cmyframe: onlbuttondown (uint nflags, cpoint point)
{
Trace ("entering cmyframe: onlbuttondown-% lx, % d, % d/N ",
(Long) nflags, point. X, point. y );
Cpaintdc DC (this );
DC. textout (0, 0, "Hello world! ");
}
After running, "Hello World!" is displayed when you click the left button! ", But when the window is minimized and then maximized" Hello world! "No.
⑧ Close the application: a series of events occur when you close the application. First, the cmyframe object is deleted, then run is exited, then winmain is exited, and then cmyapp object is deleted.
Through the above example, we can see that most of the functions of the program are included in the basic cwinapp and cframewnd classes. We only write a few functions to complete very complex functions. Therefore, the application framework is not only a Type Library, but also defines the structure of the application. Besides the base class, it also includes the winmain function, and is used to support message processing, diagnosis, DLL, and so on are included in the application framework.

 

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.