Visual C++ is an exciting subject, and this first section gets you right into the basic features of the new Visual C++ 5 compiler and starts you off building some basic programs.
These are the highlights of this section:
Windows development tools have certainly come a long way since the earliest C and C++ compilers for Windows. By combining into a single tool all the resources required to build Windows applications, Microsoft has made it much easier for you to learn to build applications.
The central part of the Visual C++ package is Developer Studio,
the Integrated Development Environment (IDE), shown in Figure 1.1. Developer
Studio is used to integrate the development tools and the Visual C++ compiler.
You can create a Windows program, scan through an impressive amount of
online help, and debug a program without leaving Developer Studio.
figure 1.1
Using Developer Studio to create a Windows program.
Visual C++ and Developer Studio make up a fully integrated environment that makes it very easy to create Windows programs. By using the tools and wizards provided as part of Developer Studio, along with the MFC class library, you can create a program in just a few minutes.
Many of the programs used as examples in this book require less than a page of additional source code. However, these programs use the thousands of lines of source code that are part of the MFC class library. They also take advantage of AppWizard and ClassWizard, two of the Developer Studio tools that manage your project for you.
In addition to tools that are used for debugging, editing, and creating resources, Developer Studio includes several wizards that are used to simplify developing your Windows programs. The most commonly used ones are
New Term: Visual C++ 5 includes Version 5.0 of MFC, the Microsoft Foundation Classes, a class library that makes programming for Windows much easier.
By using the MFC classes when writing your programs for Windows, you can take advantage of a large amount of source code that has been written for you. This enables you to concentrate on the important parts of your code rather than worry about the details of Windows programming.
New Term: A recent addition to the C++ standard is the Standard C++ Library. This library includes a set of classes that were known as the Standard Template Library, or STL, during the standardization process. Unlike the MFC class library, which is used primarily for Windows programming, the standard C++ library is used for general-purpose programming.
Starting Developer Studio from the Start button.
Developer Studio initially displays two windows:
figure 1.3
Developer Studio when first started.
Time Saver: Usually, the indexes used by the InfoViewer are copied to your hard disk and the actual database remains on the CD-ROM. If you would like to speed up InfoViewer, run Visual C++ setup again and install InfoViewer to the hard disk.
The Project Workspace window shown in Figure 1.3 is an example of a dockable view. To "undock" a dockable window, double-click the window's edge. To dock a floating window, move it to the edge of the workspace. If it is a dockable window, it docks itself. If you want to move a dockable window close to the edge of a workspace without docking, press the Ctrl key on the keyboard when moving the window.
You use the Developer Studio editor to edit C++ source files that will be compiled into Windows programs. The editor supplied with Developer Studio is similar to a word processor, but instead of fancy text-formatting features, it has features that make it easy to write source code.
You can use almost any editor to write C++ source code, but there are several reasons to consider using the editor integrated with Developer Studio. The editor includes many features that are found in specialized programming editors.
Just a Minute: If you do choose to use another editor to create your source files, make sure the files are stored as ASCII, also known as "plain text" files. The Visual C++ compiler cannot process files that have special formatting characters embedded in them, such as the files created by word- processing programs.
figure 1.4
An example of keyboard command bindings in Developer Studio.
For your first Visual C++ program, you will build a console-mode program that displays a Hello World greeting. Console-mode programs are often simpler to build than Windows applications, and this example will take you through the steps of building and running a program built with Visual C++.
2. Select the Projects tab, and then click the Win32 Console Application icon from the list box.
3. Specify Hello as the project name in the Project name box; a default location for your project will automatically be entered in the Location box (see Figure 1.5).
figure 1.5
The New Projects dialog box for the Hello project.
// Hello world example
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Open a new source file document and type the program exactly as shown in
Listing 1.1. As discussed earlier, there are two ways to open a new source
file for editing:
CAUTION: When using C++, remember that capitalization is important. For example, MAIN and main are two different names to the compiler. White space, such as the number of spaces before a word such as cout, is not significant to the compiler. White space is often used to help make programs more readable.
Just a Minute: If you used the toolbar's New Source File icon to create your new source file, syntax highlighting will not be provided until the file is saved and the file is given a name. This is because the Developer Studio editor uses the file extension to determine the file type, and it does not know what type of file is being edited.
When updating a previously saved source file, you don't see a dialog box, and no further action is needed on your part. The existing file is updated using the current contents of the editor. If you save a new file, you see the Save As dialog box, and you must choose a location and filename for the new source file. Save the contents of Listing 1.1 in the C:\ directory using the name CFoo.cpp. After saving the file, close CFoo.cpp by selecting Close from the File menu.
To save a file under a new name, select Save As from the File menu or press F12. Enter the new path and filename using the Save As dialog box as described previously.
If you have not yet added the source file to the project, follow these steps:
2. Select the Hello.cpp source file and then click OK.
Just a Minute: Visual C++ requires that your C++ source files have a .CPP file extension. This helps Developer Studio properly compile your source code, as well as provide the proper syntax highlighting.
Other types of files also have standard extensions. For example, C source files must use the .C extension. Other file extensions will be discussed as they are introduced.
HELLO.exe - 0 error(s), 0 warning(s)
Time Saver: You can also build the Hello project by clicking the Build button on the toolbar. The toolbar was shown in Figure 1.3.
If errors or warnings are displayed in the Build status window, there is probably an error in the source file. Check your source file again for missing semicolons, quotes, or braces.
C:\Program File\DevStudio\MyProjects\HelloOn some machines, filenames may be truncated, so the path on your machine might be something like
C:\progra~1\devstudio\myprojects\helloYou'll see a subdirectory named DEBUG. The Visual C++ IDE puts all the executable and intermediate files into this directory by default. Change to the DEBUG directory and execute the Hello.exe program by typing the following at the DOS prompt:
HELLOThe program loads and then displays Hello World!. That's all there is to it.
All of the console mode or DOS programs used as examples in this book should be compiled and executed just like Hello.exe. You'll always create a project, add files to it, and then build the project. After the application is built, you then go out to DOS and execute the program.
AppWizard creates all the source files required to build a skeleton Windows application. It also configures a project for you and allows you to specify the project directory. Although an AppWizard project is a skeleton of a future project, it uses the MFC class library to include the following functions:
2. Create any additional resources used by the program.
3. Add any additional classes and message-handling functions using ClassWizard.
4. Add the functionality required by your program. You actually have to write some code yourself for this part.
5. Compile and test your program, using the Visual C++ integrated debugger if needed.
2. Select the Projects tab. A list of project types will be displayed.
3. To create an MFC-based project, select MFC AppWizard(exe) as the project type.
4. Specify HelloMFC as the project name in the Project name box; a default location for your project will automatically be entered in the Location box (see Figure 1.6).
The New Projects dialog box for the HelloMFC project.
6. The first MFC AppWizard screen asks for a project type, as shown in Figure 1.7. MFC AppWizard works similarly to the Developer Studio Setup Wizard, enabling you to move forward and backward using the Next and Back buttons. Select the radio button labeled Single Document and then click the Next button.
Figure 1.7
The first AppWizard screen for HelloMFC.
8. The last MFC AppWizard screen presents a list of classes that is generated for the project. Click the button labeled Finish. MFC AppWizard displays a summary of the project, listing the classes and features you selected, as shown in Figure 1.8.

The New Project Information dialog box for the Hello project.
To edit the CHelloMFCView class, follow these steps:
2. Expand the CHelloMFCView node of the tree control. A list of functions that are used in the CHelloMFCView class will be displayed.
3. Double-click the function named OnDraw. The editor will open to the OnDraw member function. Edit the CHelloMFCView::OnDraw function so that it looks like the function in Listing 1.2. You will need to remove a comment and two existing lines of code that were in the function already.
void CHelloMFCView::OnDraw(CDC* pDC)
{
pDC->TextOut(50,50,"Hello MFC!", 10);
}
Compile the HelloMFC project by selecting Build|Build HelloMFC.exe from
the main menu (or press F7). The build window displays the progress of
the build, which should look something like the following:
Compiling resources... Compiling... StdAfx.cpp Compiling... HelloMFCDoc.cpp HelloMFC.cpp MainFrm.cpp HelloMFCView.cpp Generating Code... Linking... HelloMFC.exe - 0 error(s), 0 warning(s)Congratulations; you have created a simple Windows program! To execute the HelloMFC project, select Execute from the Build menu or press F5 on the keyboard. The most common way to launch a project from Developer Studio is to use the debugger. To start the debugger, click the Go button on the toolbar or press F5 on the keyboard.
Figure 1.9 shows an example of the HelloMFC application running under Windows 95.
Figure 1.9
The HelloMFC program.
One unusual aspect of the HelloMFC application is that the message is in a fixed location. If the window is resized, the text doesn't move. This is because the call to DrawText needs a fixed location for the message string in the first two parameters:
pDC->TextOut(50,50,"Hello MFC!", 10);The third parameter is the actual message to be displayed, and the last parameter is the number of characters in the message.
In the next section, you will learn how to display the message in the center of the main window.
You also created two small programs using Visual C++: a console-mode application that displayed "Hello World!" and a Windows application that was built with AppWizard.
A C++ is very close to C in a number of ways. Almost every legal C program is also a legal C++ program. C++ introduces the idea of classes, which are discussed in section 3. A C++ compiler also has a different standard library than a C compiler. As you will see, Visual C++ makes it very easy to develop Windows programs using C++, even if you have no experience in C or C++.
Q Can I replace the Developer Studio editor with my own favorite editor?
A No, but you can use your favorite editor to edit files, then use Developer Studio to build those files into a final executable. You will lose many of the integrated benefits of the integrated editor if you do this, however. You can change the Developer Studio editor to emulate Brief and Epsilon editors if you prefer their keyboard mappings.
2. How do you build a project using Developer Studio?
3. What is a wizard?
4. What are the three most commonly used wizards?
5. How do you invoke context-sensitive help inside the editor?
6. What are the four tab views inside the Project Workspace window?
7. What MFC function is used to display output?
8. What keyboard function is used to start the build process in Developer Studio?
9. What keyboard editor command is used for Undo?
10. What is the difference between Undo and Redo?
2. The first two parameters in the TextOut function call are the position coordinates for the text message. Experiment with the HelloMFC application, and change the position of the output message.
Download Sample Programs - HelloMFC.zip
|
|
|