编程语言

img GodSun_

VC++.NET学习笔记(第三章)

发表于2004/10/7 9:35:00  1443人阅读

3 Getting Started with the MFC Application Wizard

    Chapter 2 introduced the MFC library’s document-view architecture. This hands-on chapter will show you how to build a functioning MFC library application while insulating you from the complexities of the class hierarchy and object interrelationships. You’ll work with only one document-view program element: the view class that is closely associated with a window. For the time being, you can ignore elements such as the application class, the frame window, and the document. Of course, your application won’t be able to save its data to disk and won’t support multiple views, but Part III of this book will give you plenty of opportunity to explore using those features.

MFC应用程序向导开始

    第二章介绍了MFC的文档-视图架构.这章将向你展示并动手教你怎样用MFC函数建立应用程序,当然复杂的类层次与对象关系你是见不到的.你将建立一个仅有一个文档-视图的程序:这个视图类就是关闭关联的窗口. 暂时,你可以不理会那些例如应用程序类,框架窗口,和文档等东西.当然,你的应用程序必须能够保存数据和支持多视图, 到第三部分本书会给充足的机会来探讨和使用这些特征.

   

    Because resources are so important in Microsoft Windows-based applications, you’ll use Resource View in this chapter to visually explore the resources of your new program. The chapter will also give you some hints on setting up your Windows environment for maximum build speed and optimal debugging output.

    因为资源在基于Windows的应用程序中是非常重要的,所以你将在本章使用资源管理器并在你的程序中真正体验资源.本章同时也将提供一些设置方法来最大限度的使你的Windows环境最快,调试环境最佳.

   

    Note To compile and run the examples presented in this chapter and in the following chapters, you must have Microsoft Windows NT4.0, Windows 2000, or Windows XP installed, plus all the Microsoft Visual C++.NET components. Be sure that Visual C++.NET ’s executable, include, and library directories are set correctly. (You can change the directories by choosing Options from the Tools menu and clicking the Projects folder.) If you have any problems with the steps presented, please refer to your Visual C++.NET documentation and Readme  files for troubleshooting instructions.

    注意 当你编译和执行本章和以下章节提供的例子时,你必须在微软Windows NT4.0 Windows2000,或者Windows XP中安装微软Visual C++.net的组件.注意你必须正确的设置Visual C++.NET的执行,包含的库文件所在的目录.(通过工具菜单和单击项目文件夹你可以改变和选择其他目录).如果你遇到任何问题,请查找Visual C++.NET文档和Readme文件来寻求帮助.

 

 

What’s a View?

    From a user’s standpoint, a view is an ordinary window that the user can size, move, and close in the same way as any other Windows-based application windows. From the programmer’s perspective, a view is a C++ object of a class derived from the MFC library Cview classes. Like any C++ object, the view object’s behavior is determined by the member functions (add data members) of the class-both the application-specific functions in the derived class and the standard functions inherited from the base classes.

什么是视图?

    以用户的立场,视图是普通的窗口跟其它任何基于Windows的应用程序的窗口一样可以调整大小,移动和关闭.从程序员的角度,视图只是来自于MFC类库Cview类的C++对象而已.与任何一个C++对象类似,视图对象是由源类和继承自基类的标准函数两者组成的应用程序特定函数等成员函数决定其行为的.

   

    With Visual C++.NET, you can produce interesting applications for Windows by simply adding code to the derived view class that the MFC Application Wizard code generator produces. When your program runs, the MFC library application framework constructs an object of the derived view class and displays a window that is tightly linked to the C++ view object. As is customary in C++ programming, the view class code is divided into two source modules the header file(H) and the implementation file(CPP).

    使用Visual C++.NET,你只要使用MFC应用程序向导代码产生器产生框架,在给源视图类添加些简单的代码就可以产生非常有趣的应用程序了.当你的程序运行,MFC应用程序框架将构建一个由源视图类组成的对象并显示一个与C++视图对象紧密相连的窗口.C++语言一样,视图类代码有俩个部分组成,一个是头文件(H),一个是执行文件(CPP).

   

MFC Library Application Types

    The MFC library supports three application types: Single Document Interface(SDI), Multiple Document Interface(MDI), and Multiple Top-Level Windows Interface(MTI). An SDI application has, from the user’s point of view, only one window. If the application depends on disk-file “document”, only one document can be loaded at a time. The original Windows Notepad is an example of and SDI application. An MDI application has multiple child windows, each of which corresponds to an individual document. Earlier versions of Microsoft Office applications (before Office 2000) such as Microsoft Word are examples of MDI applications. An MTI application is a single instance of an application running several of the top-level windows. Modern versions of the Office applications use this model.

MFC应用程序的类型

    MFC支持三种应用程序类型:单文档界面,多文档界面,多顶层窗口界面.一个SDI应用程序,从用户观点,仅一个窗口.如果应用程序依靠的是磁盘文件文档”,每次就只加载一个文档.早期Windows的记事本就是SDI的例子.一个MDI有多个子窗口组成,每个都是独立的文档.早期的Office应用程序(Office2000以前)中的Word就是这种MDI的例子.一个MTI的应用程序是一个由应用程序运行个别顶层窗口的单一的实例.现在版本的Office应用程序才用这种模块.

   

    When you run the MFC Application Wizard to create a new project, MDI is the default application type. For the early examples in this book, you’ll be generating SDI applications because fewer classes and features are involved. Be sure to select the Single Document option(on the Application Type page of the MFC Application Wizard) for these examples. Starting with Chapter 18, you’ll be generating MDI applications. The MFC library application framework architecture ensures that most SDI examples can be upgraded easily to MDI applications.

    当你用MFC应用程序向导建立一个新的项目时,MDI是缺省的类型.在本书的前面的例子,你看到的都是SDI应用程序,因为只涉及到少数的类跟特征.在这些例子中你可以选择单文档选项(MFC应用向导的应用类型页).从第18章开始,你将看到MDI的应用程序.MFC应用框架确保你能看到更多的SDI的例子,同时也帮你更容易巩固MDI的应用程序.

 

 

EX03a: The “Do-Nothing” Application

    The MFC Application Wizard generates the code for a functioning MFC library application. This working application simply brings up an empty window with a menu attached. Later, you add code that draws inside the window. You take the following steps to build an application:

 

1.    Run the MFC Application Wizard to generate SDI application

Source code. Choose New Project from Visual C++ ’s File menu.

Select Visual C++ Projects, and then select MFC Application from the list of templates, as shown here.

Type C:/vcppnet/ in the Location box. Type Ex03a in the Name box, and then click OK. Use the links on the left side of the dialog box to move through the various Application Wizard pages to set up project options.

 例子03a: 一个什么都不做的应用程序

    MFC的应用程序向导可以产生代码.它只为应用程序产生一个简单的带菜单的空窗口.稍后你要自己为窗口增加代码.你可以跟随以下步骤来建立应用程序:

   

1.     运行MFC应用程序向导产生SDI应用程序的源代码

Visual C++的文件菜单中选择新的项目, 选择Visual C++项目,并在模板窗口列表中选择MFC应用程序. 如图示:

Location(位置)框中填入:C:/vcppnet/,Name(名称)框中填入:Ex03a而后单击OK.

使用左边的按钮可以在对话框中通过不同的应用程序向导页设置项目参数.

 

On the Application Type page, select the Single Document option and accept the defaults for the rest of the application, as shown here:

    On the Generated Classes page, shown below, notice that the class names and source-file names have been generated based on the project name Ex03a. You can make

Changes to these names at this point if you want to. Click Finish. The wizard will create your application’s subdirectory (Ex03a under /vcppnet) and a series of files in that subdirectory. When the wizard is finished. Look in the application’s subdirectory.

    在应用程序类型页面,选择单文档参数并保持应用程序的缺省设置,如图:

    在生成类页,在下面显示的,注意类名和源文件名都是基于项目生成的Ex03a.如果你想要改变名字可以在这里修改.单击完成.向导将在你的子目录里建立应用程序和一系列的文件(Ex03a /vcppnet).当向导完成时.看应用程序的子目录.

 

    Table 3-1 lists the files that are of interest (for now).

Table 3-1 Important Files in the Application’s Subdirectory

File               Description

Ex03a.vcproj        A project file that allows Visual C++ .NET to build your

                    application

Ex03a.sln           A solution file that contains a single entry for ex03a.vcproj

Ex03a.rc            An ASCII resource script file

Ex03aView.cpp        A view class implementation file that contains Cex03aView

                    Class member functions

Ex03aView.h         A view class header file that contains the Cex03aView class

                    declaration

ReadMe.txt          A text file that explains the purpose of the generated files

Resource.h          A header file that contains #define constant definitions

 

    Open the ex03aView.cpp and ex03aView.h files and look at the source code. Together, these files define the Cex03aView class, which is central to the application. An object of class Cex03aView corresponds to the application’s view window, where all the “action” takes place.

 

3-1列出了重要的文件(目前是)

3-1 应用程序子目录下重要的文件

文件                意义

Ex03a.vcproj        项目文件使Visual C++ .NET建立你的应用程序

Ex03a.sln           解决方案文件包含了Ex03a的简要信息.

Ex03a.rc            ASCII 资源脚本文件

Ex03aView.cpp        视图类的重要文件包含了Cex03aView类成员函数

Ex03aView.h         视图类的头文件包含了Cex03aView类的声明

ReadMe.txt          文本文件解释产生的文件的目的

Resource.h          头文件中#define常量定义

打开Ex03aView.cppEx03aView.h文件并看里面的代码,这俩个文件合起来就定义了Cex03aView的类,那是应用程序的中心.一个Cex03aView类相当于应用程序的视图窗口,所有的”action”都在那里产生.

 

2.     Compile and link the generated code. In addition to generating code, the MFC Application Wizard creates custom project and workspace files for your application. The project file, ex03a.vcproj, specifies all the file dependencies along with the compile and link option flags. Because the new project becomes Visual C++.NET ‘s current project, you can now build the application by choosing Build from the Build menu or by clicking the Build toolbar button, shown here:

If the build is successful, an executable program named Ex03a.exe will be created in a new Debug subdirectory underneath /vcppnet/Ex03a. The OBJ files and other intermediate files are also stored in Debug. Compare the files structure on disk with the structure in Solution Explorer, shown on the next page.

Solution Explorer contains a logical view of your project. The header files show up under Header Files, even though they are in the same subdirectory as the CPP files. The resource files are stored in the /res subdirectory.

 

3.     编译和连接生成的代码.除产生的代码,MFC应用程序向导为你的程序建立自定义的项目和工作空间文件.项目文件,EX03A.VCPROJ,指定所有文件都从属于编译和连接选项.因为新项目变成了当前项目,你可以通过生成菜单或单击工具栏上的生成按钮生成应用程序,如图:

如果生成完成,一个名为Ex03a.exe的可执行文件将会在/vcppnet/ex03a目录下新生成的子目录Debug.OBJ文件和其他中间文件也存储在Debug目录下.用解决方案浏览器来比较磁盘上的文件结构.图示在下一页;

解决方案浏览器为你的项目文件排列了合理的视图.头文件都放在Header Files(头文件)下面,即使跟那些CPP文件在同一个子目录下也是这样.资源文件存储在/RES子目录下.

 

3 Test the resulting application. Choose Start Without Debugging from the Debug menu. Experiment with the program. It doesn’t do much, does it?(What do you expect with no coding?) Actually, as you might guess, the program has a lot of features—you simply haven’t activated them yet. Close the program window when you’ve finished experimenting.

 

4 Browse the application. Press CTRL+ALT+J to bring up the Object Browse. If you project settings don’t specify browser database creation, Visual C++ .NET will offer to change the settings and recompile the program for you. (To change the settings yourself , choose Properties from the Project menu. Open the C/C++ folder, click on the Browse Information property page, and change the Enable Browse Information property to Include All Browse Information (/FR).)

 

3测试完成的应用程序. 如果没有调试,从调试菜单选择启动.给程序进行调试.它不会做很多事,它做什么?(你期望没有代码吗?)事实上,你可能要猜了,这个程序有许多特色,你没有灵活的使用它.当你完成调试时,关闭程序窗口.

 

4浏览应用程序. CTRL+ALT+J调出对象浏览器.如果你的项目没有建立详细的浏览器数据资料设置,Visual C++.NET将建议你改变设置并重新编译你的程序.(你也可以自己改变设置,从项目菜单中选择属性.打开C/C++文件夹,单击浏览信心属性页,在启用浏览信息属性框中选择包含所有浏览器信息(/FR).

 

     After you expand the hierarchy, you should see output similar to this:

    Compare the browser output to Class View:

Class View shows the class hierarchy, much like the Object Browser does. But the Object Browser shows all the functions available on a class, and Class View shows only those that have been overridden. If Class View is sufficient for you, don’t bother building the browser database.

     你展开层次之后,你将看到类似的界面,比较一下类视图浏览器,类视图显示的是类的层次,许多对象浏览器都是这么工作的.但是对象浏览器显示的是类内可用的函数,而类视图仅仅显示那些将被重栽的函数.如果类视图足够用了,那就不要建立浏览数据库了.

 

The Cex03aView View Class

     The MFC Application Wizard generated the Cex03aView view class, which is specific to the Ex03A application. (The wizard generates classes based on the project name you entered in the New Project dialog box) Cex03aView is at the bottom of a long inheritance chain of MFC library classes, as shown previously in the Object Browser window. The class picks up member functions and data members all along the chain. You can learn about these classes in the Microsoft Foundation Class Reference (online or printed version), but be sure to look at the descriptions for every base class because the descriptions of inherited member functions aren’t generally repeated for derived classes.

     The most important Cex03aView base classes are CWnd and Cview. CWnd  provides the Cex03aView view class’s “windowness”, and Cview provides the hooks to the rest of the application framework, particularly to the document and to the frame window, as you’ll see in Chapter 12 of this book.

Cex03aView视图类

     MFC应用程序向导生成的Cex03aView视图类,那是个详细的EX03A应用程序.(这噶向导生成的类是你在新建项目对话框中输入的项目名字)Cex03aView是从MFC类的底层的一系列类继承过来的,同样也显示在先前的对象浏览器窗口里.这个类会从一系列的数据成员中精选的成员函数.你可以从微软基础类的参考书中学习到这些类(在线帮助或手册)但是必须看每个基类的说明,因为这些说明在继承成员函数中的起源类通常不在重复的.

Cex03aView最重要的基类是CWndCview.CWnd提供CEX03aView视图类的轻窗口”,Cview提供挂靠到应用程序框架,独特的文档和框架窗口,同样的你将在本书的第12章看到.

    

Drawing Inside the View Window: The Windows GDI

     Now you’re ready to write code to draw inside the view window. You’ll be making a few changes directly to the Ex03a source code. Specifically, you’ll be fleshing out the OnDraw member function in ex03aView.cpp and working with the device context and the Graphics Device Interface(GDI).

在视图窗口中绘图:Windows GDI

     现在你准备给视图窗口写绘图代码.你将直接修改EX03a的源代码.特别的,你将从ex03aView.cpp中揭开OnDraw的成员函数并用设备上下文和图形设备接口来工作.

    

    The OnDraw Member Function

    OnDraw is a virtual member function of the Cview class that the application framework calls every time the view window needs to be repainted. A window needs to be repainted if the user resizes the window or reveals a previously hidden part of the window, or if the application changes the window’s data, if must inform Windows of the change by calling the view’s inherited Invalidate (or InvalidateRect) member function. This call to Invalidate triggers a later call to OnDraw.

    Even though you can draw inside a window at any time, it’s better to let window changes accumulate and then process them all together in the OnDraw function. That way, your program can respond both to program-generated events and to Windows-generated events such as size changes.

OnDraw成员函数

    OnDrarw CVIEW类的虚函数,每当视图窗口要重绘时应用框架就会调用它.在以下情况一个窗口需要被重绘:如果用户更改窗口大小或使先前部分隐藏的窗口在现,应用程序改变窗口数据,如果必须通知Windows来改变那么将调用视图的继承函数Invalidate.这将先触发Invalidate而后在调用OnDraw

    即使你任意时刻都在绘制窗口,它也能很好的在OnDraw函数中同时处理堆积的窗口改变的事件.因此,你的程序能响应程序与Windows俩者产生的事件例如改变大小.

   

The Windows Device Context

    Recall from Chapter 1 that Windows doesn’t allow direct access to the display hardware but communicates through an abstraction called a device context that is associated with the window. In the MFC library, the device context is a C++ object of class CDC that is passed(by pointer) as a parameter to OnDraw. After you have the device context pointer, you can call the many CDC member functions that do the work of drawing.

    重新回顾第一章,Windows没有允许你直接访问显示硬件,但是通信通过一个抽象调用设备上下文来与窗口关联.MFC,这个设备上下文是C++对象的类CDC被当做参数传个OnDraw.你拥有了这个设备上下文指针之后,你可以调用许多CDC函数来进行绘图工作.

 

Adding Draw Code to the Ex03a Program

    Now let’s write the code to draw some text and a circle inside the view window. Be sure that the project Ex03a is open in Visual C++.NET. You can use Class View to locate the code for the function (double-click OnDraw), or you can open the source code file ex03aView.cpp from Solution Explorer and locate the function yourself.

1.     Edit the OnDraw function in ex03aView.cpp. Find the Application Wizard-generated OnDraw function in ex03aView.cpp:

void CEx03aView::OnDraw(CDC* /*pDC*/)

{

    CEx03aDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    if (!pDoc)

        return;

    // TODO: 在此处为本机数据添加绘制代码

}

Uncomment the pointer to the device context and add the following boldface code(which you type in)to replace the previous code:

void CEx03aView::OnDraw(CDC* /*pDC*/)

{

    pDC->TextOut(0,0,Hello,world!);    //prints in default font

                                    //& size. Top left cornet

    pDC->SelectStockObject(GRAY_BRUSH); //selects a brush for the

                                    //circle interior

    pDC->Ellipse(Crect(0,20,100,120));  //draws a gray circle

                                    //100 units in diameter

}

 

Ex03a程序增加绘图代码:

    现在让我门给视图窗口写一些绘制文字和圆形的代码.请确认在Visual C++.NET中打开Ex03a这个项目.你可以用类视图定位到函数的代码(双击OnDraw),或者你从解决资源管理器选择并打开源代码文件ex03aView.cpp,自己定位到函数上去.

1.     编辑ex03aView.cpp的OnDraw函数.找到应用程序向导产生的ex03aView.cpp文件中的OnDraw函数:

void CEx03aView::OnDraw(CDC* /*pDC*/)

{

    CEx03aDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    if (!pDoc)

        return;

    // TODO: 在此处为本机数据添加绘制代码

}

去除设备上下文的指针的注释,用增加的黑体字的代码替换先前的代码:

void CEx03aView::OnDraw(CDC* pDC)

{

    pDC->TextOut(0,0,Hello,world!);    //打印缺省字体

                                    //和大小. 顶部,左边短号

    pDC->SelectStockObject(GRAY_BRUSH); //选择一个圆形的画刷

                                    //在里面绘制一个

    pDC->Ellipse(Crect(0,20,100,120));  //灰色的圆形

                                    //直径100个单位

}

 

    You can safely remove the call to GetDocument because were not dealing with documents yet. The functions TextOut, SelectStockObject, and Ellipse are all member functions of the application frameworks device context class CDC. The Ellipse function draws a circle if the bounding rectangles length is equal to its width

    The MFC library provides a handy utility class, CRect, for Windows rectangles. A temporary CRect object serves as the bounding rectangle argument for the ellipse drawing function. Youll see more of the CRect class in quite a few of the examples in this book.

    你可以放心的删除GetDocument函数,因为我们不需要文档.函数TextOut,SelectStockObject,Ellipse都是应用框架设备上下文类CDC的成员函数.Ellipse函数是绘制圆形的如果要绘制矩形长度等于宽度就行.

    MFC提供了一个易得到的,有用的类,CRect,可以为Windows画矩形.一个临时的CRect对象在Eillpse绘图函数里同样也能绘制矩形.你将在本书中看到更多CRect类完整的例子.

 

2 Recompile and test Ex03a. Choose Build from the Build menu, and if there are no compile errors, test the application again. Now you have a program that visibly does something!

2重编译和测试Ex03a. 选择生成菜单的生成,如果你的应用程序在次测试没有编译错误.现在你的程序显然能做某些事了.

阅读全文
0 0

相关文章推荐

img
取 消
img