Part 2 windows programming and Object-Oriented Programming

Source: Internet
Author: User

Http://www.cnblogs.com/inshion/archive/2009/01/10/1372887.html

2.1 Windows programming review
Speaking of Windows programming, simply put, Windows APIs are called for Windows applications.Program. For example, you can draw a window, write a menu, put a button, and respond to the mouse. Basically, all the related tutorials will use the following small example to demonstrate the simplest windows application:

// ------------ Start ---------------
# Include...

// Callback function
LresultWndproc(){
//Switch msg
...
Wm_paint:
Onpaint ();
...
}

Void onpaint () {// draw}

// Entry
Int winmain (){
// 1: register the window class and adjust the callback function of the windowWndproc
// 2: Create and display a window
// 3: Enter the message loop

}


// ------------ End ---------------


as shown in the preceding program, normal Windows Programming involves steps 1, 2, and 3. In this way, a win form will be generated during the runtime, and the system and various messages sent to it by
users will be continuously received, such as clicking, moving, and closing. Then, we can write our own functions. For example, if an image is displayed every time a window is clicked, or if a user clicks the
button, a prompt box is displayed, and so on. These functions must be added to the switch statement of wndproc . This creates a problem.
"do I need to change it frequently? wndproc or What About The called function (such as onpaint?"
OK. Generally, this is the case. This causes a lot of trouble for development. For example, you need to modify the same Code Frequently. For example, every time you create an application, the above structure must be repeated... And so on
. As a result, we are depressed and begin to consider how we can come up with a permanent solution, so that the above three steps can be solidified and will be developed later. We only need to write functions, you do not need to repeat or modify the written content. Fortunately, the object-oriented development method provides us with a good solution.

2.2 graphic program structure
After analysis, we found that the window application generally has the following structure.
That is, the entry winmain uses a global application *
Application to call the initinstance () method of the application, while application: initinstance ()
The window: Create () method is also called. In this way, the window is created, and the system and user messages, such as wm_paint, can be passed by the corresponding window
Processing functions. In this way, the underlying program is completed. Simply put, this is a Windows application that has no function but can accept various messages.

To add our own functions, we only need to inherit the application and window classes as shown in. AndBefore Running winmain, point the global application to the MyApp instance.In this way, all the messages in the new program will be transferred to our own mywin class for processing, and we can add our own functions as we like.

At this point, some people may have a question: "Before Running winmain, let the global application point to the MyApp instance "?Winmain is not a program entry. How can we perform other operations before it is executed?
You do not need to answer this question. If you have any questions, runBelow is a small programSee the output to understand why there are many operations available before winmain:

Code 001
//------------ Start ---------------
# Include<Iostream>
Using NamespaceSTD;

Int Main (){
Cout <   " I am the first line in main (). I will always be the first output! "   < Endl; // Prints !!! Hello world !!!
Return   0 ;
}
Class A {
Public :
A (){
Cout < " Haha, that's not necessarily the case. I will execute it earlier than main ~ " < Endl;
}
};
A;
// -------------- End ---------------


2.3 encapsulate windowsapi with objects
the focus of object-oriented programming is the need for abstraction.
let's take a look at the establishment process of the above Windows program in three steps, so we will summarize it and write it into these parts.
// ----------------------------
class app {
app () {APP = This ;}
Init (); // call Create method
Run (); // 3: enter the message loop
window * mainwin;
}< br> app * app;
// --------------------------

// ----------------------------
class window {
Create (); // 1: Registration window class, and adjust the callback function of the window to wndproc 2: Create and display a window
virtual wndproc (); /// map a message to a specific processing function, such as wm_paint, to onpaint ()
virtual onpaint ();
...
}< br> // ----------------------------

//----------------------------
// Entry
Int winmain (){
App-> Init ();
App-> Run ()
}
//----------------------------
OK. What does this structure mean? Simply put, the application degree is made into a class, and the window is made into a class. The program entry calls the init () method of the program. This method calls the CREATE () method of the window and finally calls the run () method of the program to load the program.
What are the benefits of doing so?
The answer is: You don't need to change the code at all. You only need to add new code, and the new Code only needs to focus on its new functions, without knowing where the message comes from, where can we develop our own applications!
Why?
For example. (Pay attention to the app pointer app and APP = this in the app constructor, and then look at the several virtual functions .)
For example, to create an application, you can write the following class:

//----------------------------
MyApp theapp;
MyApp: Public app {
Window win;
Init () {win. Create (); mainwin = & Win ;}
}
//----------------------------

//----------------------------
Mywin: public window {
Onpaint ();
Onclose ();
...
}
//----------------------------

as long as this is done, we can implement our own functions in the onxxxx () method of mywin. The classes previously compiled can not be changed, you can call the entire program process. We don't even need the Source Code above, you only need to introduce the compiled library and header file to expand the file.
the entire call process is that app instantiation is completed by MyApp theapp. Because app constructor contains APP =
This ;, therefore, the app points to the MyApp instance. When the program receives a message, it finds the corresponding processing function through wndproc (for example, wm_paint corresponds to the
onpaint () method), and then calls mainwin-> onpaint (). Because the mainwin instance is generated in MyApp: Init (),
the instance is mywin, and mywin reloads the virtual onpaint, therefore, the call is adjusted to the mywin: onpaint () method.
In summary, this actually completes the process of encapsulating windowsapi processes into C ++ classes , classes and objects are used to build a bridge between user calls and windowsapi. Here is a topic about MFC. what does MFC do, is such a encapsulate the windowsapi process into a C ++ class Process . Of course, its structure is more rigorous and complex, and there are more things to consider. It is far from a sentence or two, but its ideas and procedures are encapsulated in the same way as the above process.
at this point, we have understood how to solve the problem of Windows programs with object-oriented thinking. In the next section, I provide a complete C ++ Program (eclipse + CDT) , summarize some problems that may be encountered during compilation and running in eclipse to complete the content described in this section.

Related Article

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.