VC ++. NET learning notes (continued)

xiaoxiao2021-03-06  67

The Learning Curve

All the benefits sound great, do not they? But you're probably thinking, "You do not get something for nothing." Yes, that's true. To use the application framework effectively, you have to learn it thoroughly, and that takes time. If you had to learn C , Windows, and the MFC library (without OLE) all at the same time, it would take at least six months before you were really productive. Interestingly, that's close to the learning time for the Win32 API Alone.

Learn content

All great and beneficial sounds are not them? But you are thinking about it, "You can't get it alone". Yes, then true. If you want to effectively use the application framework, you have to spend a lot of time thoroughly Learn it. If you have learned C , Windows and MFC (excluding OLE) before, they can take a real product. Interestingly, learning time is close to Win32API time.

How can that be if the MFC library offers so much more? For one thing, you can avoid many programming details that C-language Win32 programmers are forced to learn. From my own experience, I can say that and object-oriented application framework makes Programming for Windows Easier To Learn --- Than IS, Once You Und Object-Oriented Programming.

How to make MFC more valuable? First, you can avoid the details that many Win32 C language programmers are not facing. From my past experience, writing object-oriented procedures under Windows is very easy to learn, of course, you have learned to object-oriented programming.

The MFC library will not bring real Windows-based programming to the masses. Programmers of applications for Windows have usually commanded higher salaries than other programmers, and that situation will continue. The MFC library's learning curve, together with the application framework's power, should Ensure That MFC Library Program Will Continue To Be in Strong Demand.

The MFC is a truly developed Windows program tool. Developing a Windows program programmer is higher than those in the command line mode, and the status will be higher. Learning MFC, in addition to the power of the application framework will guarantee usage MFC's programmers provide a stronger demand.

What's an application framework?

One definition of application framework is "an integrated collection of object-oriented software components that offers all that's needed for a generic application." That is not a very useful definition, is it? If you really want to know what an application framework is You'll Have to Read The Rest of this book. The Application Framework Example That You'll Look At Later In this chapter is a good start point. What is the application framework?

One definition of the application framework is: "Integrate the components of the object-oriented software to be supplied to a universal application." This is a very good definition, why, if you really want to know what is the application framework, you know the book carefully. You will be in this chapter to see the example of the framework of the application, you can let You have a good start.

`

An Application Framework vs. a Class Library

One reason that C is a popular language is that is can be "extended" with class libraries. Some class libraries are delivered with C compilers, others are sold by third-party software firms, and still others are developed in-house. A class library is a set of related C classes that can be used in an application. A mathematics class library, for example, might perform common mathematics operations, and a communications class library might support the transfer of data over a serial link. Sometimes you construct objects Of The Supplied Classes, Sometimes You Derive Your OWN Classes-It All De Dends on The Design of The Particular Class Library.

Comparison of application frameworks and class libraries

The reason why C can become a general language is that it can be unlimited. C compiler contains some class libraries, some third-party manufacturers are selling, while others have developed at home. A class library sets a relationship in the C class to use it in your application. For example, a data class library can complete a universal data operation, a communication class library supports multiple connections for data transfer. Sometimes you need to construct an object by supporting classes. Sometimes you need to come from your own class - all this is the unique class design.

An application framework is a superset of a class library. An ordinary library is an isolated set of classes designed to be incorporated into any program, but an application framework defines the structure of the program itself. Microsoft did not invent the application framework concept. It first appeared in the academic world, and the first commercial version was MacApp for the Apple Macintosh. Since the MFC library 2.0 was introduced, other companies, including Borland, have released similar products. application framework is an extension library. In any of the programs, classes are combined with a number of separate libraries, but in the application framework defined structure is the program itself. Microsoft does not invent the concept of application framework. The first commercial version is in the academic world, the first commercial version is MacApp in the Apple's Mac system, which later has MFC2.0, other companies, such as Borland, also released similar products.

An Application Framework EXAMPLE

Enough generalizations. It's time to look at some code-not pseudocode but real code that actually complies and runs with MFC library. Guess what? It's the good old "Hello, world!" Application, with a few additions. (If you've used version 1.0 of the MFC library, this code will be familiar except for the frame widows base class.) It's about the minimum amount of code for a working MFC library application for Windows. (Contrast it with an equivalent pure Win32 application such as you Would See In a petzold book!) You don't have to understand every line now. don't bother to type it in and test it, because ex21b on the Companion CD is quite similar. Wait for the next chapter, where you ' LL Start Using The "Real" Application Framework.

An example of an application framework

A very common example. It's time to see some code - there is no pseudo code, it is a real code, it is to compile and run on the MFC library. Guess what? It is an old "Hello, World!" Program, (if you have used MFC1.0, then this program is the most familiar to those frameworks). He probably can run the least amount of code on the MFC framework. (Some of the pure Win32 applications, such as seeing in Petzold). You don't need to understand each line of code now. You don't have to Worried about its type and test it because the EX21b has the same example in the CD of the book. Wait until the next chapter, you will see the real application framework. Note by Convertion, MFC Library Class Names Begin with The Letter C.

Following is the source code for the header and implementation files for our MYAPP application The classes CMyApp and CmyFrame are each derived from MFC library base classes First, here is the MyApp.h header file for the MYAPP application..:

Note According to management, the name of the MFC comes from C

Source code file from the beginning of our MyApp program. CMMYAPP and CMYFRAME are classic and MFC library bases. First, here is the header file MyApp.h of myApp programs:

// Application Class

Class CMYAPP: Public CWINAPP

{

PUBLIC:

Virtual Bool InitInstance ():

}

// Frame Window Class

Class CMYFRAME: PUBLIC CFRAMEWND

{

PUBLIC:

CMYFRAME ();

Protected:

// "AFX_MSG" INDICES That The Next Two Functions Are PART

// of the mfc library message dispatch system

AFX_MSG Void OnlButtondown (Unit Nflag, CPoint Point);

AFX_MSG void onpaint ();

Declare_message_map ()

}

And here is the myapp.cpp usteation file for the myapp application;

And here is the executable of myApp program myApp.cpp.cpp

#include // MFC Library Header File Declares Base Classes

#include "myapp.h"

CMYAPP THEAPP; // The One and Only CMYAPP OBJECT

Bool CmyApp :: InitInstance ()

{

m_pmainwnd = new cmyframe ();

m_pmainwnd-> showwindow9m_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 (Unit Nflags, Cpoint Point)

{

TRACE ("Entering CMYFRAME :: ONLBUTTONDOWN -% 1x,% D,% D / N", (long) nflags, point.x, point.y;

}

Void CMYFrame :: onpaint ()

{

CPAINTDC DC (this);

Dc.Textout (0, 0, "Hello, World!");

}

Here Are Some of the Program Elements:

Here is some elements contained in this program:

The WinMain function

Windows Requires Your Application To Have to Winmain Function. You Don't SEE Winmain Here Because It's Hidden Inside The Application Framework.

Winmain function

WindWos requests your application through the Winmain function, you can't see the Winmain function because it is hidden in the application framework.

The CmyApp Class

An Object of Class CMYAPP Represents and Application. The Program Defines A Single Global CMYAPP Object, Theapp. The cwinapp base class determines..

CMYAPP class

The CMYAPP class object is a declaration of the application. ThecmyApp object defines a global object TheApp.cwinapp.cwinapp base class in the program is the behavior of defining theApp object.

Application Startup

When the user starts the application, Windows calls the application framework's built-in WinMain function, and WinMain looks for your globally constructed application object of a class derived from CwinApp. Do not forget that in a C program global objects are constructed before the main Program is executed.

Application startup

When the user launches the application, Windows calls the Winmain function that is bound to the application framework. This from the CWINAPP function will look for the global constructed application object in your program. Don't forget the C program constructor to execute before your master program .

The cmyapp :: InitInstance Member Function

When the WinMain function finds the application object, it calls the virtual InitInstance member function, which makes the calls needed to construct and display the application's main frame window. You must override InitInstance in your derived application class because the CwinApp base class does not know What Kind of Main Frame Window You Want.

CMYAPP :: InitInstance Member Function

When the WinMain function finds an application object, it calls virtual functions initInstance, then constructs and displays the application main window. You must overload InitInstance in your application, because the cwinapp base class does not know what kind of main window you want.

The CwinApp :: Run member functionThe Run function is hidden in the base class, but it dispatches the application's messages to its windows, thus keeping the application running. WinMain calls Run after it calls InitInstance

CWINAPP :: Run Member Function

The RUN function is hidden in the base class, but the information will be sent to WindWos, so keep the application run .WinMain is called Run after calling InitInstance.

转载请注明原文地址:https://www.9cbs.com/read-119943.html

New Post(0)