版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机专业毕业设计外文翻译Visual C+ MFC 简要介绍工 学 部 工学一部专 业计算机科学与技术班 级学 号姓 名指导教师负责教师沈阳航空工业学院北方科技学院2008年7月Introduction to MFC Programming with Visual C+ Version 6.x by Marshall Brain Visual C+ is much more than a compiler. It is a complete application development environment that, when used as intended, lets you fu
2、lly exploit the object oriented nature of C+ to create professional Windows applications. In order to take advantage of these features, you need to understand the C+ programming language. If you have never used C+, please turn to the C+ tutorials in the C/C+ Tutorials page for an introduction. You m
3、ust then understand the Microsoft Foundation Class (MFC) hierarchy. This class hierarchy encapsulates the user interface portion of the Windows API, and makes it significantly easier to create Windows applications in an object oriented way. This hierarchy is available for and compatible with all ver
4、sions of Windows. The code you create in MFC is extremely portable.These tutorials introduce the fundamental concepts and vocabulary behind MFC and event driven programming. In this tutorial you will enter, compile, and run a simple MFC program using Visual C+. Tutotial 2 provides a detailed explana
5、tion of the code used in Tutorial 1. Tutorial 3 discusses MFC controls and their customization. Tutorial 4 covers message maps, which let you handle events in MFC.What is the Microsoft Foundations Class Library? Let's say you want to create a Windows application. You might, for example, need to
6、create a specialized text or drawing editor, or a program that finds files on a large hard disk, or an application that lets a user visualize the interrelationships in a big data set. Where do you begin?A good starting place is the design of the user interface. First, decide what the user should be
7、able to do with the program and then pick a set of user interface objects accordingly. The Windows user interface has a number of standard controls, such as buttons, menus, scroll bars, and lists, that are already familiar to Windows users. With this in mind, the programmer must choose a set of cont
8、rols and decide how they should be arranged on screen. A time-honored procedure is to make a rough sketch of the proposed user interface (by tradition on a napkin or the back of an envelope) and play with the elements until they feel right. For small projects, or for the early prototyping phase of a
9、 larger project, this is sufficient.The next step is to implement the code. When creating a program for any Windows platform, the programmer has two choices: C or C+. With C, the programmer codes at the level of the Windows Application Program Interface (API). This interface consists of a collection
10、 of hundreds of C functions described in the Window's API Reference books. For Window's NT, the API is typically referred to as the "Win32 API," to distinguish it from the original 16-bit API of lower-level Windows products like Windows 3.1.Microsoft also provides a C+ library that
11、 sits on top of any of the Windows APIs and makes the programmer's job easier. Called the Microsoft Foundation Class library (MFC), this library's primary advantage is efficiency. It greatly reduces the amount of code that must be written to create a Windows program. It also provides all the
12、 advantages normally found in C+ programming, such as inheritance and encapsulation. MFC is portable, so that, for example, code created under Windows 3.1 can move to Windows NT or Windows 95 very easily. MFC is therefore the preferred method for developing Windows applications and will be used thro
13、ughout these tutorials.When you use MFC, you write code that creates the necessary user interface controls and customizes their appearance. You also write code that responds when the user manipulates these controls. For example, if the user clicks a button, you want to have code in place that respon
14、ds appropriately. It is this sort of event-handling code that will form the bulk of any application. Once the application responds correctly to all of the available controls, it is finished.You can see from this discussion that the creation of a Windows program is a straightforward process when usin
15、g MFC. The goal of these tutorials is to fill in the details and to show the techniques you can use to create professional applications as quickly as possible. The Visual C+ application development environment is specifically tuned to MFC, so by learning MFC and Visual C+ together you can significan
16、tly increase your power as an application developer.Windows Vocabulary The vocabulary used to talk about user interface features and software development in Windows is basic but unique. Here we review a few definitions to make discussion easier for those who are new to the environment.Windows applic
17、ations use several standard user controls:Static text labels Push buttons List boxes Combo boxes (a more advanced form of list) Radio boxes Check boxes Editable text areas (single and multi-line) Scroll bars You can create these controls either in code or through a "resource editor" that c
18、an create dialogs and the controls inside of them. In this set of tutorials we will examine how to create them in code. See the tutorials on the AppWizard and ClassWizard for an introduction to the resource editor for dialogs.Windows supports several types of application windows. A typical applicati
19、on will live inside a "frame window". A frame window is a fully featured main window that the user can re-size, minimize, maximize to fill the screen, and so on. Windows also supports two types of dialog boxes: modal and modeless. A modal dialog box, once on the screen, blocks input to the
20、 rest of the application until it is answered. A modeless dialog box can appear at the same time as the application and seems to "float above" it to keep from being overlaid.Most simple Windows applications use a Single Document Interface, or SDI, frame. The Clock, PIF editor, and Notepad
21、are examples of SDI applications. Windows also provides an organizing scheme called the Multiple Document Interface, or MDI for more complicated applications. The MDI system allows the user to view multiple documents at the same time within a single instance of an application. For example, a text ed
22、itor might allow the user to open multiple files simultaneously. When implemented with MDI, the application presents a large application window that can hold multiple sub-windows, each containing a document. The single main menu is held by the main application window and it applies to the top-most w
23、indow held within the MDI frame. Individual windows can be iconified or expanded as desired within the MDI frame, or the entire MDI frame can be minimized into a single icon on the desktop. The MDI interface gives the impression of a second desktop out on the desktop, and it goes a long way towards
24、organizing and removing window clutter.Each application that you create will use its own unique set of controls, its own menu structure, and its own dialog boxes. A great deal of the effort that goes into creating any good application interface lies in the choice and organization of these interface
25、objects. Visual C+, along with its resource editors, makes the creation and customization of these interface objects extremely easy.Event-driven Software and Vocabulary All window-based GUIs contain the same basic elements and all operate in the same way. On screen the user sees a group of windows,
26、each of which contains controls, icons, objects and such that are manipulated with the mouse or the keyboard. The interface objects seen by the user are the same from system to system: push buttons, scroll bars, icons, dialog boxes, pull down menus, etc. These interface objects all work the same way
27、, although some have minor differences in their "look and feel." For example, scroll bars look slightly different as you move from Windows to the Mac to Motif, but they all do the same thing.From a programmer's standpoint, the systems are all similar in concept, although they differ ra
28、dically in their specifics. To create a GUI program, the programmer first puts all of the needed user interface controls into a window. For example, if the programmer is trying to create a simple program such as a Fahrenheit to Celsius converter, then the programmer selects user interface objects ap
29、propriate to the task and displays them on screen. In this example, the programmer might let the user enter a temperature in an editable text area, display the converted temperature in another un-editable text area, and let the user exit the program by clicking on a push-button labeled "quit&qu
30、ot;.As the user manipulates the application's controls, the program must respond appropriately. The responses are determined by the user's actions on the different controls using the mouse and the keyboard. Each user interface object on the screen will respond to events differently. For exam
31、ple, if the user clicks the Quit button, the button must update the screen appropriately, highlighting itself as necessary. Then the program must respond by quitting. Normally the button manages its appearance itself, and the program in some way receives a message from the button that says, "Th
32、e quit button was pressed. Do something about it." The program responds by exiting.Windows follows this same general pattern. In a typical application you will create a main window and place inside it different user interface controls. These controls are often referred to as child windows-each
33、control is like a smaller and more specialized sub-window inside the main application window. As the application programmer, you manipulate the controls by sending messages via function calls, and they respond to user actions by sending messages back to your code.If you have never done any "eve
34、nt-driven" programming, then all of this may seem foreign to you. However, the event-driven style of programming is easy to understand. The exact details depend on the system and the level at which you are interfacing with it, but the basic concepts are similar. In an event-driven interface, th
35、e application paints several (or many) user interface objects such as buttons, text areas, and menus onto the screen. Now the application waits-typically in a piece of code called an event loop-for the user to do something. The user can do anything to any of the objects on screen using either the mo
36、use or the keyboard. The user might click one of the buttons, for example. The mouse click is called an event. Event driven systems define events for user actions such as mouse clicks and keystrokes, as well as for system activities such as screen updating.At the lowest level of abstraction, you hav
37、e to respond to each event in a fair amount of detail. This is the case when you are writing normal C code directly to the API. In such a scenario, you receive the mouse-click event in some sort of structure. Code in your event loop looks at different fields in the structure, determines which user i
38、nterface object was affected, perhaps highlights the object in some way to give the user visual feedback, and then performs the appropriate action for that object and event. When there are many objects on the screen the application becomes very large. It can take quite a bit of code simply to figure
39、 out which object was clicked and what to do about it.Fortunately, you can work at a much higher level of abstraction. In MFC, almost all these low-level implementation details are handled for you. If you want to place a user interface object on the screen, you create it with two lines of code. If t
40、he user clicks on a button, the button does everything needed to update its appearance on the screen and then calls a pre-arranged function in your program. This function contains the code that implements the appropriate action for the button. MFC handles all the details for you: You create the butt
41、on and tell it about a specific handler function, and it calls your function when the user presses it. Tutorial 4 shows you how to handle events using message mapsAn Example One of the best ways to begin understanding the structure and style of a typical MFC program is to enter, compile, and run a s
42、mall example. The listing below contains a simple "hello world" program. If this is the first time you've seen this sort of program, it probably will not make a lot of sense initially. Don't worry about that. We will examine the code in detail in the next tutorial. For now, the goa
43、l is to use the Visual C+ environment to create, compile and execute this simple program./hello.cpp#include <afxwin.h>/ Declare the application classclass CHelloApp : public CWinApppublic: virtual BOOL InitInstance();/ Create an instance of the application classCHelloApp HelloApp;/ Declare the
44、 main window classclass CHelloWindow : public CFrameWnd CStatic* cs;public: CHelloWindow();/ The InitInstance function is called each/ time the application first executes.BOOL CHelloApp:InitInstance() m_pMainWnd = new CHelloWindow(); m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow
45、(); return TRUE;/ The constructor for the window classCHelloWindow:CHelloWindow() / Create the window itself Create(NULL, "Hello World!", WS_OVERLAPPEDWINDOW, CRect(0,0,200,200); / Create a static label cs = new CStatic(); cs->Create("hello world", WS_CHILD|WS_VISIBLE|SS_CENTE
46、R, CRect(50,80,150,150), this);This small program does three things. First, it creates an "application object." Every MFC program you write will have a single application object that handles the initialization details of MFC and Windows. Next, the application creates a single window on the
47、 screen to act as the main application window. Finally, inside that window the application creates a single static text label containing the words "hello world". We will look at this program in detail in the next tutorial to gain a complete understanding of its structure.The steps necessar
48、y to enter and compile this program are straightforward. If you have not yet installed Visual C+ on your machine, do so now. You will have the option of creating standard and custom installations. For the purposes of these tutorials a standard installation is suitable and after answering two or thre
49、e simple questions the rest of the installation is quick and painless.Start VC+ by double clicking on its icon in the Visual C+ group of the Program Manager. If you have just installed the product, you will see an empty window with a menu bar. If VC+ has been used before on this machine, it is possi
50、ble for it to come up in several different states because VC+ remembers and automatically reopens the project and files in use the last time it exited. What we want right now is a state where it has no project or code loaded. If the program starts with a dialog that says it was unable to find a cert
51、ain file, clear the dialog by clicking the "No" button. Go to the Window menu and select the Close All option if it is available. Go to the File menu and select the Close option if it is available to close any remaining windows. Now you are at the proper starting point. If you have just in
52、stalled the package, you will see a window that looks something like this:This screen can be rather intimidating the first time you see it. To eliminate some of the intimidation, click on the lower of the two "x" buttons () that you see in the upper right hand corner of the screen if it is
53、 available. This action will let you close the "InfoViewer Topic" window. If you want to get rid of the InfoViewer toolbar as well, you can drag it so it docks somewhere along the side of the window, or close it and later get it back by choosing the Customize option in the Tools menu.What
54、you see now is "normal". Along the top is the menu bar and several toolbars. Along the left side are all of the topics available from the on-line book collection (you might want to explore by double clicking on several of the items you see there - the collection of information found in the
55、 on-line books is gigantic). Along the bottom is a status window where various messages will be displayed. Now what? What you would like to do is type in the above program, compile it and run it. Before you start, switch to the File Manager (or the MS-DOS prompt) and make sure your drive has at leas
56、t five megabytes of free space available. Then take the following steps.Creating a Project and Compiling the Code In order to compile any code in Visual C+, you have to create a project. With a very small program like this the project seems like overkill, but in any real program the project concept
57、is quite useful. A project holds three different types of information:It remembers all of the source code files that combine together to create one executable. In this simple example, the file HELLO.CPP will be the only source file, but in larger applications you often break the code up into several
58、 different files to make it easier to understand (and also to make it possible for several people to work on it simultaneously). The project maintains a list of the different source files and compiles all of them as necessary each time you want to create a new executable. It remembers compiler and l
59、inker options particular to this specific application. For example, it remembers which libraries to link into the executable, whether or not you want to use pre-compiled headers, and so on. It remembers what type of project you wish to build: a console application, a windows application, etc. If you
60、 are familiar with makefiles, then it is easy to think of a project as a machine-generated makefile that has a very easy-to-understand user interface to manipulate it. For now we will create a very simple project file and use it to compile HELLO.CPP.To create a new project for HELLO.CPP, choose the New option i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 感应性妄想性障碍的临床护理
- 腰椎增生的临床护理
- 飞鸽传书linux课程设计
- 合同- 合作协议
- 飞机装配框缘条课程设计
- 风速监测系统课程设计
- 风刀干燥系统课程设计
- 教师职业技能大赛实施方案
- 非遗美术手工课程设计
- 非遗文化 特色课程设计
- 发生心脏骤停的应急预案
- 咸阳中心医院门诊综合楼装修改造项目施工组织设计
- 全国高考数学新课标Ⅱ卷第11题说题课件
- 人教版九年级英语全一册Unit5大单元教学设计
- 2021版集成电路技术专业群人才培养方案
- 新软件推广营销方案
- 生物免疫与疫苗研究
- 湘美版四年级美术上册每课知识要点汇总
- 创意摄影实训智慧树知到期末考试答案2024年
- 冲上云霄-飞机鉴赏智慧树知到期末考试答案2024年
- 初中数学指导青年教师工作计划
评论
0/150
提交评论