"Visual C MFC Concise Tutorial" Original: Marshall Brain Compilation: Zhang Shenghua
Part 1: MFC Introduction Visual C is more than just a compiler. It is a comprehensive application development environment that uses it you take advantage of C with object-oriented features to develop professional Windows applications. In order to make full use of these features, you must understand the C programming language. Master C , you must master the hierarchy of the Microsoft Basic Class Bank (MFC). This hierarchy is tolerate the user interface part in the Windows API and enable you to easily establish a Windows application in an object-oriented manner. This hierarchy is suitable for all versions of Windows and compatible with each other. The code created with MFC is fully portable. This tutorial will introduce you to the basic concepts and terms of the MFC and the design method of event driver. In this section, you will enter, compile and run a simple MFC program. These code will be explained in detail in the next section. The third part discusses the MFC control and how to customize them. The fourth section describes the message mapping and you will process the MFC event. What is MFC? If you want to build a Windows application, how should you start? A good start is from the design user interface. First, you have to decide what kind of user can use the program and set the corresponding user interface object as needed. The Windows user interface has some standard controls, such as buttons, menus, scroll bars, and lists, which are very familiar with those Windows users. To remember, as a programmer must choose a set of controls and decide how to arrange them on the screen. Traditionally, you need to do the sketch of the user interface on the paper until you are satisfied with each element. This is possible for some of the relatively small projects, as well as the early prototyping phase of some major projects. The next step is to implement the code. When establishing an application for any Windows platform, both programmers have two options: C or C . Use C, programmers write code on the Windows Application Interface (API). This interface is composed of hundreds of C functions, which are introduced in the Windows API reference manual. For Windows NT, the API is called "WIN32 API" to distinguish between 16-bit APIs used for Windows 3.1. Microsoft also provides a C library, which is located on any Windows API that makes programmers' work easier. It is the Microsoft Basic Class Library (MFC), the main advantage of this library is efficient. It reduces the code that must be written in a large number of WINDOWS programs. At the same time, it also provides all the best C programming advantages, such as inheritance and packaging. The MFC is portable, for example, code written under Windows 3.1 can be easily ported to Windows NT or Windows 95. Therefore, the MFC is worth recommending to develop a Windows application method, which is MFC in this tutorial. When using the MFC, the code you have written is used to establish the necessary user interface control and customize its appearance. At the same time, you have to write code to respond to users to operate these controls. For example, if a user clicks on a button, you should have a code to respond. This is the event-driven code, which constitutes all applications. Once the application is correctly responding to all allowed controls, its task is also completed. You can see that when Windows programming is used by MFC, it is a relatively easy process. The purpose of this tutorial is to compare how to quickly establish a professional-grade application technology.
The Visual C application development program is particularly suitable for using MFC (also using MFC, translator's note), so learning MFC and Visual C can enhance your development program. Words whose Windows vocabulary in Windows user interfaces and software development are basic and unique. For users who are new to this environment, review several definitions below to make our discussion easier. Windows application uses several standard control: Static text tab button list box combo box (a more advanced list box) radio button Check button edit box (single line and multi-line) scroll bar You can edit it through code or "Resources "To create these controls, the dialogs and these controls can be established in the resource editor. In this tutorial, we will use the code to build them. Windows supports several types of application windows. A typical application should be active in the "Frame Window". A frame window is a full-featured main window, and the user can change the size, minimize, maximize, and so on. Windows also supports two types of dialogs: mode and non-Mode dialog. Once the Mode dialog box appears on the screen, only the rest of the application on the screen can respond when it exits. When the Mode dialog box appears on the screen, the rest of the program can also respond, it is like floating above. The simplest Windows application is using a single document interface (SDI), with only one frame window. Windows watch, PIF editor, notepad, etc. are examples of SDI applications. Windows also provides an organizational form called multi-document interface that can be used for more complex applications. The MDI system allows users to view multiple documents at the same time. For example, a text editor can allow users to simultaneously open multiple text files. When using MDI, the application has a main window that has some sub-windows in the main window, each containing their own documents. In the MDI framework, the main window has a main menu, which is valid for the top window in the main frame. Each sub-window can be reduced to the icon or expand, and the MDI main window can also become an icon on the desktop. The MDI interface may give you a second desktop feel, it has a great help to the management and delete the chaotic window. You have established a application that uses its own control, menu structure, and dialog. The good and bad application interface depends on how you choose and organize these interface objects. The resource editor in Visual C allows you to build and customize these interface objects. Event Driver Software and Vocabulary All Windows-based GUIs contain the same basic elements, their operational methods are the same. On the screen, the user sees a set of windows, each window contains control, icons, objects, and some elements that handle mouse and keyboard. From a user point of view, the interface objects of each system are the same: buttons, scroll bars, icons, dialogs, and drop-down menus, and more. Although the "appearance and feelings" of these interface elements may differ, these interface objects have the same way. For example, scroll bars may be somewhat different for Windows, Mac and Motif, but their role is completely. From a programmer's point of view, these systems are conceptually similar, although they may have a lot. In order to establish a GUI program, the programmer should put all the required user interface controls on the window. For example, if the programmer is to establish a simple program from the transition from Celsius, the user interface selected by the programmer is completed and displayed on the screen. In this simple program, the programmer may need the user to enter the temperature value in an editable editing box, display the conversion result in an inable editable edit box, and then let the user can click a button labeled "exit". To exit the application. Because it is the user to operate the application, the program must respond. The response is dependent on the user using the mouse or keyboard in different controls.
Each user interface object on the screen is different for the response of the event. For example, if a user clicks an exit button, the button must update the screen and highlight itself. The program then must respond to exit. The modes used by Windows are also similar. In a typical application, you will create a main window and place some user interface controls. These controls are often referred to as sub-windows they are like some smaller and more special sub-windows in the main window. As a programmer, you should call to send information operations through a function to send these controls to respond to the user's operation by sending information to your code. If you have never done an event driver design, all of these may be very strange to you. However, the event driver design is easy to understand. Specific details may differ from different systems, but its basic concept is similar. In an event-driven interface, the application draws several interface objects on the screen, such as buttons, text areas, and menus. Applications typically respond to the user's operation through a code called an event cycle. Users can use the mouse or keyboard to operate the object on the screen. For example, the user clicks a button with a mouse. Clicking with a mouse is called an event. The event drive system defines the user's actions such as the mouse, and the keyboard operation is defined as an event, and the system operation is also defined as an event. In a relatively low-level programming method, if you write a Windows API application directly with C, the amount of code is very large, because the details you have to take care of it too much. For example, you use some type of structure to receive clicked mouse events. The code in your event loop will view different domains in the structure to determine which user interface is subject to influence, and then the corresponding operation is completed. Applications become big when there are many objects on the screen. Just simply handle which object is clicked and what you need to do what you need to do. Fortunately, you can program in a relatively advanced approach, which is using MFC. In MFC, almost all low-level details are handed over. If you put a user interface object on the screen, you only need two lines of code to create it. If the user clicks on a button, the button will complete all the necessary operations, from the update screen to the pre-process function in your program. This function contains a code that makes a corresponding operation of the button. MFC handles all the details: You build buttons and tell it specific handle, when it is pressed, it calls the corresponding function. The fourth part describes how to use a message mapping to handle events. Examples Understand the structure and style of a typical MFC program are to enter a small program, then compile and run it. The following program is a simple "Hello World" program. This is very familiar with many C programmers, let's take a look at how to use the MFC method. If you see such a program for the first time, it may be more difficult to understand. This doesn't matter, we will introduce it in detail later. Now you can build, compile and run it in the Visual C environment.
///hello.cpp #include // Description Application Class Class ChelloApp: Public CWINAPP {public: Virtual Bool InitInstance ();}; // Establish an instance of the application class ChelLoApp HelloApp; // Description Main window class CHELLOW: PUBLIC CFraMewnd {cstatic * cs; public: chellowindow ();}; // Whenever the application is called by the initialization function BOOL CHELLOAPP :: InitInstance () {m_pmainwnd = new chellowindow (); m_pmainwnd-> showwindow (m_ncmdshow ); M_pmainwnd-> UpdateWindow (); Return True;} // window class constructor Chellowindow :: chellowindow () {// Create window itself Create (Null, "Hello World!", WS_OVERLAPPEDWINDOW, CRECT (0,0,200,200) ); // Establish a static tab CS = new cStatic (); CS-> Create ("Hello World", WS_CHILD | WS_VISIBLE | SS_CENTER, CRECT (50, 80, 150, 150), this);} If this program above, if you use C Realize, you have to need a few pages of code. This simple example did three things. First, it established an application object. Each MFC program you have written has a single program object that processes the initial details of MFC and Windows. Second, the application establishes a window to be used as the main window of the application. Finally, a static text tag is established in the application's window, which contains several words of "Hello World". In the second part we will study this process carefully to understand its structure. Start VC , if you just install it, you will see an empty window with the toolbar on the screen. If VC has been used on the machine, the displayed window may be somewhat different because VC will re-open items and files when you exit after the last use. What we need is that it is not installed as any project and code. If the program is started, the pop-up dialog indicates that some files cannot be opened, you can click "NO". Select the "Close All" option in the "Window" menu to close all windows. Select "Close" option in the "File" menu to close other windows. Now you are in the beginning. If you are running after installing VC , the screen should be as follows: If you don't want to see the "InfoViewer Topic" window in the future, you can turn it off with the button. If you need it later, you can also click the "Home" button on the toolbar to open the window. Everything is everything is normal. As you can see, the top is a menu and several toolbar. The left window is displayed online help, you can double-click on a title to browse its content. The content of online help is very rich. What should I do now? What you have to do is entering the above program, then cheap and run it. Before you start, check at least 5MB of remaining space on your hard drive. Establishing a project and compile code In order to compile the code in Visual C , you have to create a project. In order to build a small program, it is possible to have a bit small matter, but in any actual program, the concept of the project is very useful.
One project mainly saves the following three different types of information: it can remember to create all source program code files required for executable programs. In this simple example, the file hello.cpp is the only source file, but in a large application, in order to facilitate management and maintenance, you can have many different source files. The project maintains a list of these different files and compiles them when you want to create the next new executable. It will remember the compiler and connector options used for your application. For example, it remembers which library connects to the executive, and whether you preparatively translate the header file and so on. It will remember the type of project you want to build: a console application, or a window application, etc. If you have already understood project files, it will be easy to understand the role of project files generated by the machine. Now let's build a simple project and use it to compile hello.cpp. To do this, first select the "New" option from the "File" menu. In the "Projects" tab, add "Win32 Application". Enter a suitable path name or click the "Browse" button to select one in the "Location" field. Enter "Hello" as the project name in "Project Name". At this time you will see "Hello" will also appear in the "location" domain. Click the "OK" button. Visual C will create a new directory called Hello and put all items file hello.opt, hello.ncb, hello.dsp, and hello.dsw in this directory. If you quit, you can reopen the project later, you can choose Hello.dsw. Now, three tags appear on the left side of the screen. The InfoView tag is still in, and there is a new ClassView and FileView tags. The ClassView tab will list all the classes in your program, and the FileView tab gives a list of files in the project. You can now enter the code of the program. Select the "New" option in the "File" menu to create an editing window. In the dialog box that appears, select the "Files" tab and "text file". The Visual C intelligent editor will appear, you can use it to enter the above program code. When you enter a code, you will find that the editor will automatically turn different types of text into different colors, such as comments, keyword strings, etc. color. If you want to change its color or turn the color function, select the "Options" option in the Tools menu, then select the "Format" tab and "Source Windows" option to modify. After entering the code, select the "Save" option in the "File" menu to save. In the newly established directory of Visual C , the Hello.cpp file is stored. Now select "Add to Project" option in the "Project" menu, then select "Files ...". You will see a dialog for you to choose the file you want to add. In this example, select the Hello.cpp file. On the left side of the screen, click the FileView tab and double-click the icon marked with Hello. You will see files named hello.cpp. Click the ClassView tab and double-click the folder icon, you will see all the classes in the program. At any time, you can use FileView to delete the project file, you just click on the file, then press the Delete key on the keyboard. After that, you must tell the project to use the MFC library. If you ignore this step, the project will be wrong when the project is connected, and the error message is unable to help you. Select "Settings" of the "Project" menu. Select the "General" tab in the dialog that appears.
In the "Microsoft Foundation Classes" combo box, select "Use MFC in A Shared DLL". Then close the dialog box. We have established project files and adjusted settings, you can now prepare to compile the Hello.cpp program. In the "Build" menu, you will find three different compile options: compile hello.cpp (only when the window containing hello.cpp is activated) Build Hello.exe rebuild all the first option just compiled Files and form their target files. This option cannot complete the connection task, so it is only useful to quickly compile some source files to check the error. The second option compiles all the source files modified after the last compiled, and connects to the formation executable. The third option To recompile and connect all source files. We can choose "Build Hello.exe" to compile and connect code. Visual C will establish a new child directory named "debug" and put hello.exe in this directory. The files of the subdirectory can be generated, so you can delete them any. If you find a compilation error, double-click the error message in the output window. At this time, the editor will take you to the wrong location. Check if your code has a problem, if you have, you will modify it. If you see a large number of connection errors, you may not be right in the project type specified in the Establishment Project dialog. You can delete the subdirectories where the project is located, then re-press the steps above. To perform the program, you can choose the "EXECUTE Hello.exe" option in the "Build" menu. You can see your first MFC program - a window with "Hello World" appears. The window itself has: title bar, size zoom area, maximum and minimum buttons, and more. On the window, there is a "Hello World". Please note that the program is complete. You can move the window, zoom window, minimize, and so on. You only use a very little code to complete a complete Window application. This is the advantage of using the MFC. All detail issues have MFC to handle. Conclusion In this lecture, you have successfully compiled and implemented your first MFC program. You will use similar steps to build applications. You can establish a separate directory for each project, or create a separate project file, then add or delete different source files. In the next lecture, we will carefully study the program, you will understand its structure more complete. Back to the directory Next chapter >>