A Simple Windows Application

Visual C++ offers several types of projects you can work with. When you first launch Visual C++ you will be looking at a blank Visual C++ environment window, much like the one depicted in Figure 15.1.

click to expand
Figure 15.1: The Visual C++ environment.

You then choose File from the drop-down menu, then select New. You will be given the opportunity to choose one of the many project types available to you in Visual C++. This will look similar to Figure 15.2.

click to expand
Figure 15.2: Visual C++ project types.

As you can see, there are several types of projects you can create with Visual C++. Table 15.1 summarizes the most commonly used project types and their purpose.

Table 15.1: Visual C++ Project Types

VC++ Project Type


ATL COM Appwizard

ATL is short for Active Template Library. It is a quicker way of developing COM components. These are beyond the scope of this book.

Database Project

This one should be relatively easy to understand. It is simply a project that supports connection to a database.

Dev Studio Add-In Wizard

This type of project is for developing add-ins to Microsoft Developer Studio…this is also beyond the scope of this book.

ISAPI Extension Wizard

ISAPI is a set of function calls used to create Internet applications.

MFC ActiveX Control Wizard

Active X controls are the small components you use in Windows applications (buttons, drop-down menus, etc.). This project type is for creating your own Active X components.

MFC App Wizard (DLL)

DLLs are Dynamic Linked Libraries, or groups of functions with no user interface that are used by other programs.

MFC App Wizard (exe)

This type of project is for standard executables.

Utility Project

A container for files you build without a linking step.

Win32 Application

A non-active X 32-bit Windows application.

Win32 Console Application

This project type is for creating standard C++ apps that run in the DOS window/command prompt.

Win32 Dynamic Link Library

These project types are also DLL projects, but they don’t use Active X as their basis.

Win32 Static Library

This project type is for creating static library files with functions.

Our first example will be a dialog application that will illustrate the basics of the Visual C++ Application wizard, Windows messaging, and using components in Visual C++.

Table 15.2: App Wizard Application Types

Application Type



These are the simplest types of applications and usually consist of a single screen with a few components on it.

SDI (Single Document Interface)

A single document interface allows you to view a single item or document at a time. Notepad and Wordpad are SDI applications.

MDI (Multiple Document Interface)

A multiple document interface will allow you to view multiple items/documents simultaneously. Microsoft Word is an MDI application.

Example 15.1

Step 1: Open Visual C++, choose new, and pick the MFC App Wizard (exe) type. Name it 15_01.

click to expand
Figure 15.3: App Wizard first screen.

Step 2: On the next screen (Figure 15.4), choose the dialog application type then press the next button.

click to expand
Figure 15.4: App Wizard second screen.

click to expand
Figure 15.5: App Wizard third screen.

Step 4: For the fourth screen (Figure 15.6), again use the default values.

click to expand
Figure 15.6: App Wizard fourth screen.

Step 5: For the fifth screen (Figure 15.7), again use the default settings.

click to expand
Figure 15.7: App Wizard final screen.

Now simply click finish and you will be done with the wizard. We will see in just a moment what the wizard has done for you. At this point, you will be looking at a screen much like the one depicted in Figure 15.8.

click to expand
Figure 15.8: App Wizard sixth screen.

Visual C++’s application wizard has created the basic dialog box for you, with a lot of code (which we will get to in a short while). You should also notice the floating toolbox. In it are several basic components that you can put on your dialog box (or in other programs) to provide basic Window’s functionality.

Step 6: The basic components in that toolbox will be explained in just a bit. For now, find the Edit box, it should be the one on the top right, and click on it. Then draw an edit box on your dialog box. When you place your mouse over any component in the toolbox, a tag tip will appear telling you what component that is. This is shown in Figure 15.9.

click to expand
Figure 15.9: Toolbox tag tips.

Next find the button component and place one on the dialog box. Then click once on the text that says "TO DO:…." Delete this text. When you are done, your dialog box should look much like the one shown in Figure 15.10.

click to expand
Figure 15.10: First dialog application.

Now you have finished with the visual components of your dialog box . . . now for the coding part. Note that coding Windows is complex; however, there are wizards to do a lot of this work for you.

Step 7: If you will place your mouse over the Edit box and right click on it you will see a pop-up menu with several choices. Select the class wizard and you will be presented with a screen much like the one depicted in Figure 15.11.

click to expand
Figure 15.11: The class wizard.

Step 8: You will need to select the second tab, the one labeled member variables, and then we will click the add variable button on the righthand side. You will see something much like what is shown in Figure 15.12. Give the variable a name such as mystring or mytext.

click to expand
Figure 15.12: Adding member variables.


The m_ preceding variable names is simply to indicate that these are member variables (i.e., members of some class).

Step 9: You can then click OK to dismiss that window. Next, return to the first tab, the one labeled message maps. Here we are going to create a function that is associated with the button class and which responds to the Windows message buttonclick. This means that when someone clicks that button, and Windows sends out a buttonclick message, our function will respond to it. When you select the button, you will see an image much like what is shown in Figure 15.13. When you choose add function, you will be taken to a screen that looks like Figure 15.14.

click to expand
Figure 15.13: Selecting a component in class wizard.

click to expand
Figure 15.14: Adding a new function.

You can then click OK on that window, and then OK on the class wizard window. You will be taken back to the main screen.

Step 10: Right-click on the button then select properties. You will be presented with a window where you can change the various properties.

Change the caption to read Click Me. It should look like what you see in Figure 15.15.

click to expand
Figure 15.15: The properties window.

Step 11: Double-click on the button. It will open to the function we created with the class wizard. You will add the following two lines of code.

UpdateData(TRUE); AfxMessageBox(m_mytext);

Now run your program. (You can choose Build from the drop-down menu, then select Execute, or you can simply click on the red exclamation mark on the toolbar.) Write anything you wish in the Edit box, then click the button. You should see something like what is shown in Figure 15.16.

click to expand
Figure 15.16: Running your first dialog application.

Congratulations! You have just written your first Visual C++ program! And we were able to cover a lot of fundamental material about Visual C++ programming in the process. However, this leads to a number of questions that we will now explore.

First, we should discuss the class wizard. Everything in Visual C++ is associated with a class. The class wizard simply gives you a quick and painless way to associate variables and functions to the classes used for various components (like buttons and edit boxes). We will examine the class wizard in detail later in this chapter. For now you should understand that it is essentially a wizard that makes it easy for you to associate components with variables and with functions.

When you associate a component with a variable, that component’s contents can be placed in the variable. Also the contents of the variable can be placed in the component. The way you move the contents of the variable to the component is to call the method UpdateData(FALSE). This makes all components update their data to their associated variables. If you call the method UpdateData(TRUE), then all components get data from their associated variables. You will see the UpdateData method frequently in the code samples presented in this chapter and the next. The next item you should notice about the preceding example is the message box function, which will be explained in the next section.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net