Welcome to Hour 1 of Teach Yourself Visual C++ 5 in 24 Hours! Visual C++ is an exciting subject, and this first hour 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 hour:
Visual C++ 5 is the latest C++ compiler from Microsoft, continuing a long line of Microsoft tools for Windows development. The Visual C++ package contains more than a compiler; it also contains all the libraries, examples, and documentation needed to create applications for Windows 95 and Windows NT.
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.
New Term: An IDE, or Integrated Development Environment, is a program that hosts the compiler, debugger, and application-building tools.
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.
Once upon a time, Windows programmers used simple text editors and tools that were hosted on MS-DOS to create their Windows programs. Developing a program under those conditions was tedious and error-prone. Times have definitely changed; Developer Studio includes a number of tools that you might once have paid extra to purchase.
Developer Studio also features an online help system, which can be used to get context-sensitive help for all of the tools included in Developer Studio, as well as detailed help on the C++ language, the Windows programming interface, and the MFC class library.
New Term: A Wizard is a tool that helps guide you through a series of steps.
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: A library is a collection of source code or compiled code that you can reuse in your programs. Libraries are available from compiler vendors such as Microsoft, as well as from third parties.
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.
To start Developer Studio, click the Developer Studio icon located in the Visual C++ folder. To get to the Visual C++ folder, click the Start button on the taskbar and then select Programs. One of the items in the Programs folder is Microsoft Visual C++ 5.0. Figure 1.2 shows a start menu tree opened to the Microsoft Developer Studio icon.
Figure 1.2.
Starting Developer Studio from the Start button.
Developer Studio initially displays two windows:
Developer Studio also includes a rich set of menus, toolbars, and other user interface features, as shown in Figure 1.3.
Figure 1.3.
Developer Studio when first started.
InfoViewer is the online help system integrated into Developer Studio. InfoViewer is also compatible with the Microsoft Developer Network CD-ROM, enabling you to search that database for information.
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.
New Term: Many of the views displayed by Developer Studio are dockable, which means they can be attached to the edge of the Developer Studio workspace, where they remain until undocked.
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.
To get context-sensitive help from InfoViewer, press F1. You select a topic based on the current window and cursor position, and you see the InfoViewer window, containing context-sensitive help. If you press F1 while editing a source file, help is provided for the word under the cursor. If there is more than one possible help topic, you see a list of choices.
Developer Studio includes a sophisticated editor as one of its tools. The editor is integrated with the other parts of Developer Studio; files are edited in a Developer Studio child 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.
A large set of editing commands are available from the keyboard. Although most editor commands are also available from the menu or toolbar, the following commands are frequently used from the keyboard:
This is only a small list of the available keyboard commands. To see a complete list, select Keyboard Map... from the Help menu. A list of the current keyboard command bindings is displayed, as shown in Figure 1.4.
Figure 1.4.
An example of keyboard command bindings in Developer Studio.
New Term: A console-mode application is a character-based program that runs in a DOS window.
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++.
The first stage in writing your first Visual C++ program is to create a project. Follow these steps:
Figure 1.5.
The New Projects dialog box for the Hello project.
The most important parts of any C++ program are the source files. Although the sample program provided in Listing 1.1 is very short, it contains many of the elements present in all C++ programs.
// 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:
If you open a new file for editing while a project is open, you have the option of automatically adding the file to the currently open project. To take advantage of this option, make sure the Add to Project: check box is checked, and provide a name for the file in the dialog box (in this case use Hello.cpp).
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.
After you have entered the program in Listing 1.1, save the source file in your project's directory as Hello.cpp. To save the contents of the editor, click the Save icon on the toolbar. The Save icon looks like a small floppy disk. You can also press Ctrl+S or select Save from the File menu.
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:
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.
Compile the Hello project by selecting Build|Build Hello.exe from the main menu (or press F7). If you entered Listing 1.1 correctly, the project is built with no errors, and the last line in the status window reads as follows:
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.
To run the Hello program, open a DOS window and change the working directory to the project's directory. By default, this directory is
C:\Program File\DevStudio\MyProjects\Hello
On some machines, filenames may be truncated, so the path on your machine might be something like
C:\progra~1\devstudio\myprojects\hello
You'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:
HELLO
The 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 is a tool that generates an MFC project based on options that you select. AppWizard creates all the source files required to make a skeleton project that serves as a starting point for your program. You can use AppWizard to create single-document, multiple-document, or dialog box-based applications.
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:
After answering a few questions using AppWizard, you can compile and run the first version of your application in a few minutes.
In general, the following steps are used to build a program using AppWizard:
To start AppWizard and create your first Windows program, follow these steps:
Figure 1.6.
The New Projects dialog box for the HelloMFC project.
Figure 1.7.
The first AppWizard screen for HelloMFC.
Figure 1.8.
The New Project Information dialog box for the Hello project.
After you create the HelloMFC project using MFC AppWizard, the Project Workspace window opens. The Project Workspace window contains four tabs, each used to show a different view of the current project:
The HelloMFC project already contains a function that handles output. It's called OnDraw, and it can be found in the CHelloMFCView class. When your project is created by AppWizard, the OnDraw function really doesn't do much useful work--it's up to you to supply a version of this function that does something meaningful.
To edit the CHelloMFCView class, follow these steps:
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 hour, you will learn how to display the message in the center of the main window.
In this chapter, you were introduced to Developer Studio and Visual C++, as well as the main tools and wizards included in Developer Studio and the MFC class library.
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.
The Workshop is designed to help you anticipate possible questions, review what you've learned, and begin thinking ahead to putting your knowledge into practice. The answers to the quiz are in Appendix B, "Quiz Answers."
© Copyright, Macmillan Computer Publishing. All rights reserved.