Teach Yourself Visual C++ 5 in 21 Days

Chapter 1: Writing Your First Visual C++ Application


The first chapter in the text concerns an overview of making a Windows based application. The objectives important to this chapter are:

  • creating directories and projects
  • using the Visual C++ interface
  • planning and creating visual design elements
  • adding program code to visual elements
  • modifying an icon for your executable

Chapter 1 begins with the idea that you have two major steps to take in creating a Windows based application. In reality, these are not steps, but ongoing tasks that you continue to work on throughout the creation of an application. The two things you have to keep in mind are:

  • visual design
  • program code

No program will run properly without the programmer paying attention to it, writing, testing and debugging the actual code. This is true whether we are writing a DOS, Windows or UNIX based application. The visual design aspect of a Windows application, however, is something found only in the GUI world. We are using a Visual development tool, and need to plan what the application will look like as much as what it will do.

On page 4, we are given a look at what goes into the visual design of a program. Typically, a program is written for a user who may be a client, an employer, or a market. Through some means (interview, survey, focus groups) the user is asked to specify what the program must do and how it must look. The designer then creates a design to match the user's requirements. On page 4, we can assume the designer has used an art program to mock up some screens, and shown them to the user for approval. (This can generate a cycle of refining the requirements, and the design, back and forth, until both parties can agree.)

Having an approved design, we proceed to creation of the objects and code, beginning with creating a directory to store the project files. (As he did in the appendices, the author assumes you know how to create a directory.)

Moving ahead to pages 6 and 7, we will be creating a Windows application this time, so we choose MFC App Wizard(exe) as the kind of project. (This stands for Microsoft Foundations Class Wizard. The exe just means we are creating an executable program. The rest means we are using Microsoft C++, in a visual arena, and getting help from our programming environment (Visual C++) in having it do some of the dirty work.

There are more choices to make when creating a Windows application. On pages 8 and 9, you are told to choose Dialog based and the type of application you are making. You may be aware that using a Windows dialog box is how you tell Windows or a Windows application what to do or how to do it. This application will start with a dialog box.

The other settings through page 13 set how the application will be made, as specified in the text. It is important that you get to a screen that looks like to bottom of page 13. From there, you can proceed to page 14, compile and link the application, and run it if there are no errors. (There should be no errors at this point, since you have not typed any code yet. Anything is possible, however...)

Note that the box on page 15 advises you that you can build your exe file with either the Build or Rebuild All commands from the Build menu. I usually prefer Rebuild All, since it leaves less to chance. If you have followed the text, at this point you can run the program, and will be disappointed at how little it does. Don't be disappointed! If you were writing the C++ code to put a dialog box on the screen from scratch, you would be writing it for the next couple of hours. Trust me, this is better.

So far, you haven't had direct control of much. That changes in the next section. On pages 18 and 19, you see that you should change your Workspace. By clicking the Resource View tab at the bottom of the Project Workspace, you can see a hierarchical view of the objects in your project. Your project is called Hello, so the Resources are all under Hello Resources. This appears as a folder you double click to open or close. To modify how this project works, the author asks you to open the Dialog folder, in which you see two oddly named objects. Their names start with ID and they are both dialog boxes, so the next letter in their names is a D as well. If you double-click one, the visual objects it contains appear for you to edit. The author suggests you should open the IDD_HELLO_DIALOG object. When you do, a toolbar appears to help you create more objects. That's handy, because next you delete all the existing objects in the main box, and create two new ones. Not all of the objects in the toolbar will be familiar to you right now. This time you are just creating buttons, using the button tool, the third down on the right column on pages 23 and 24.

You should be aware of the author's advice about designing visual objects: it is often easier to do so in full screen mode. To get to full screen mode, select the dialog box you are editing, click on the View menu and click Full Screen. To go back to regular view, just press Escape.

You should practice accessing the properties of an object, as instructed on pages 25 and 26. You are to select the button you created, and right click on it. You get a context sensitive menu, the last choice of which is Properties. Change the object's ID (name) and Caption properties as directed. Be aware that an object's ID and the Caption that appears on the object do not have to be at all similar. You continue the exercise by sizing the button and creating another the same size, changing its ID and Caption properties as well.

You compile and run the program. Is anyone surprised that the buttons don't do anything? There is no code associated with them. That comes next. In the same environment as above, select a button, right click, and select Class Wizard to get at a screen to begin assigning code. Before you can write code for a Windows object, you have to decide something. For a button object, the decision will be simple. Windows code is associated with events. On the screens on pages 30 and 31, the words events and messages are used interchangeably. An event is something that happens to an object, such as when a user clicks it. A message is generated and sent to Windows, telling it about the event. On pages 30 and 31, you are taught to select an object from the list on the left, and a possible event from the list on the right. What comes next? (This is C++ remember?) We click a button to Add a Function to the program. This function will be part of a class, so it is a member function, and the next step is to name it. The default name is fine. Accepting the name will take you to a text window where you can type actual code. (Sorry, the old skills are still important.)

The code in this case is at the bottom of page 33:

MessageBox("Hello. More words here.");

This is a function that C++ already knows how to execute. The MessageBox function paints a message box on the screen, with the message we supply as the parameter. The default appearance for a MessageBox is simply to have on button in it, the OK button. Since we have not specified otherwise, that is what this line will do.

On the next three pages, code is added to the Exit button you created. The only odd thing is the function you are asked to add to the button. The function is


This function basically works to close the program because it acts as if you had clicked an OK button on the dialog box that your Exit button was drawn on. Clicking OK on a dialog box closes it, so this button will close this dialog box when clicked.

The projects you create in Visual C++ will have icons associated with them. A default icon is supplied, and you are not meant to love it. Pages 39 through 42 go through a means of accessing the project icon, and customizing its color and appearance.

No one finishes a large project in one sitting, and most projects need revision from time to time. The method of opening an existing project is discussed on page 43. It is very simple:

  1. Start Visual C++
  2. Click the File menu
  3. Choose Open Workspace
  4. Drill down to the project directory you want and open the .DSW file

To shorten the instructions necessary to create a project, now that you know the basics, look over the Properties Table on page 43. You should be able to read it now, and create a project to the specifications listed. For example this line:


should be read as "Create a Dialog Box object, whose ID property is set to IDD_HELLO_DIALOG." The progression is "object, property, value".