VC++.NET学习笔记(续)

类别:编程语言 点击:0 评论:0 推荐:

The Learning Curve

    All the benefits sound great, don’t they? But you’re probably thinking, “You don’t 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.

 

学习的内容

    所有伟大而有益的声音,都不是他们?但是你大概在想,”你不可能不劳而获”.是的,那时真的.如果你想有效的使用应用程序框架,你不得不花大量的时间彻底的学习它.如果你以前同时学过C++, Windows和MFC(不包括OLE),它们最少需要你花六个月的时间才能弄出个真实的产品.有趣的是,学习的时间接近与Win32API的时间.

   

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 understand object-oriented programming.

    怎么使MFC更加有价值呢? 首先,你能避免许多Win32的C语言程序员不的不面对的细节。从我以往的经验,在windows下写面向对象的程序是非常容易学习的,当然,你曾经学习过面向对象的程序设计.

   

    The MFC library won’t 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 programmers will continue to be in strong demand.

    MFC是真正的开发Windows程序的工具,开发WINDOWS程序的程序员比那些在命令行模式下的程序员工资要高,并且地位会更高.学习MFC,在加上应用程序框架的力量将保证使用MFC的程序员提供更强壮的需求.

 

    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 isn’t 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 starting point.

    什么是应用框架?

    应用程序框架的一个定义是:”将需要的面向对象的软件的组件整合起来提供给通用的应用程序”。这是个非常好的定义,为什么呢,如果你真的想知道什么是应用程序框架,你认真的阅读本书就知道了.你将在本章的最后看到一些应用程序的框架的例子,可以让你有个好的开始.

`

    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 depends on the design of the particular class library.

    应用程序框架与类库的比较

    C++能成为通用语言的理由是它能被无限扩展.C++的编译器包含了一些类库,有些第三方厂家在出售,而有些则在家里开发。一个类库在C++类中设置一下关系就可以在你的应用程序中使用。例如,一个数据类库,可以完成通用的数据运算,一个通信类库可以支持多个连接进行数据传输。有时你需要通过支持类来构造对象。有时你需要来自你自己的类—这一切是依靠独特的类库设计。

 

    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 didn’t 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.

    应用程序框架是类库的扩展。在任何一个程序中,类是有许多独立的库组合而成的,但是在应用程序框架定义的结构中是程序自身。微软不可能发明应用程序框架的概念。最先出现在学术界,第一个商业版是APPLE的MAC系统的中的MacApp,后来有了MFC2。0,其他公司,如Borland,也发布了类似的产品.

   

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.

    一个应用框架的例子

    一个非常普遍的例子。是时候看一些代码了­—没有伪代码,是真正的代码,是可以编译和运行在MFC库上的。猜是什么? 是一个很古老的“Hello,world!“程序,(如果你用过MFC1。0,那么这个程序是你除那些框架之外最熟悉的了)。他大概是可以运行在MFC框架上代码数量最少的程序了.(跟那些纯WIN32的应用程序相等,例如你将在Petzold的书中看到的).你现在不需要明白每一行代码.你不必担心它的类型和测试它,因为EX21B在随书的CD中有个相同的例子。等到下一章,你将看到真正的应用框架。

 

Note By convention, 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:

   

注解 按管理,MFC的类名字来自与C

   

    从头开始执行我们的MYAPP程序的源代码文件.CmyApp和CmyFrame是来者与MFC库基类的.首先,这儿是MYAPP程序的头文件MyApp.h:

   

    //application class

    class CMyApp: public CWinApp

    {

    Public:

        Virtual BOOL InitInstance():

    };

   

    //frame window class

    class CMyFrame: public CFrameWnd

    {

    public:

        CMyFrame();

    Protected:

        //”afx_msg” indicates 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 implementation file for the MYAPP application;

 

    和这里是MYAPP程序MyApp.cpp的可执行文件

#include <afxwin.h> //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:

    这里是一些该程序包含的元素:

 

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函数

    Windwos通过WinMain函数来请求你的应用程序,你看不到WinMain函数的,因为它被隐藏在应用程序框架中.

   

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 most of the theApp object’s behavior.

CmyApp类

    CmyApp类对象是应用程序的声明.CmyApp对象在程序中定义了一个全局对象theApp.CwinApp基类是定义theApp对象的行为的.

 

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. Don’t forget that in a C++ program global objects are constructed before the main program is executed.

   

应用程序启动

    当用户启动应用程序,Windows调用应用程序框架里绑定的WinMain函数,这个来自CWinApp函数会寻找你程序里全局构造的应用程序对象.不要忘了C++程序构造的对象是在你主程序之前执行的.

   

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 doesn’t know what kind of main frame window you want.

 

CmyApp::InitInstance成员函数

    当WinMain函数找到应用程序对象,它调用虚函数InitInstance,接着构造和显示应用程序主窗口.你必须在你的应用程序中重载InitInstance,因为CwinApp基类不知道你想要什么样的主窗口.

   

The CwinApp::Run member function

    The 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成员函数

    Run函数是隐藏在基类的,但是会发送程序的信息给Windwos,因此保持着应用程序的运行.WinMain是在调用了InitInstance之后调用Run的.

   

本文地址:http://com.8s8s.com/it/it27858.htm