Today, your focus will be on learning about the Visual C++ development environment and some of the tools that it provides for building applications. Although Visual C++ provides more tools than you would probably use in any one application development effort--even more than you could possibly learn to use in a single day--I limit the focus to the primary tools that you will use throughout this book, as well as in just about every application you build with Visual C++. Today, you'll learn about the following:
Before you begin your quick tour around the Visual C++ development environment, you should start Visual C++ on your computer so that you can see firsthand how each of the areas are arranged and how you can change and alter that arrangement yourself.
After Developer Studio (the Microsoft Visual development environment) starts, you see a window that looks like Figure 1.1. Each of the areas has a specific purpose in the Developer Studio environment. You can rearrange these areas to customize the Developer Studio environment so that it suits your particular development needs.
When you start Visual C++ for the first time, an area on the left side of Developer Studio looks like it is taking up a lot of real estate and providing little to show for it. This area is known as the workspace, and it is your key to navigating the various pieces and parts of your development projects. The workspace allows you to view the parts of your application in three different ways:
FIGURE 1.1. The Visual C++ opening screen.
The Output pane might not be visible when you start Visual C++ for the first time. After you compile your first application, it appears at the bottom of the Developer Studio environment and remains open until you choose to close it. The Output pane is where Developer Studio provides any information that it needs to give you; where you see all the compiler progress statements, warnings, and error messages; and where the Visual C++ debugger displays all the variables with their current values as you step through your code. After you close the Output pane, it reopens itself when Visual C++ has any message that it needs to display for you.
The area on the right side of the Developer Studio environment is the editor area. This is the area where you perform all your editing when using Visual C++, where the code editor windows display when you edit C++ source code, and where the window painter displays when you design a dialog box. The editor area is even where the icon painter displays when you design the icons for use in your applications. The editor area is basically the entire Developer Studio area that is not otherwise occupied by panes, menus, or toolbars.
The first time you run Visual C++, three toolbars display just below the menu bar. Many other toolbars are available in Visual C++, and you can customize and create your own toolbars to accommodate how you best work. The three toolbars that are initially open are the following:
The WizardBar toolbar enables you to perform a number of Class Wizard actions without opening the Class Wizard.
The Developer Studio provides two easy ways to rearrange your development environment. The first is by right-clicking your mouse over the toolbar area. This action opens the pop-up menu shown in Figure 1.2, allowing you to turn on and off various toolbars and panes.
FIGURE 1.2. Toolbar on and off menu.
Another way that you can easily rearrange your development environment is to grab the double bars at the left end of any of the toolbars or panes with the mouse. You can drag the toolbars away from where they are currently docked, making them floating toolbars, as in Figure 1.3. You can drag these toolbars (and panes) to any other edge of the Developer Studio to dock them in a new spot. Even when the toolbars are docked, you can use the double bars to drag the toolbar left and right to place the toolbar where you want it to be located.
FIGURE 1.3. Example of a floating minibar.
NOTE: On the workspace and Output panes, the double bars that you can use to drag the pane around the Developer Studio environment might appear on the top of the pane or on the left side, depending on how and where the pane is docked.
For your first Visual C++ application, you are going to create a simple application that presents the user with two buttons, as in Figure 1.4. The first button will present the user with a simple greeting message, shown in Figure 1.5, and the second button will close the application. In building this application, you will need to do the following things:
FIGURE 1.4. Your first Visual C++ application.
FIGURE 1.5. If the user clicks the first button, a simple greeting is shown.
Every application development project needs its own project workspace in Visual C++. The workspace includes the directories where the application source code is kept, as well as the directories where the various build configuration files are located. You can create a new project workspace by following these steps:
FIGURE 1.6. The New Wizard.
The AppWizard asks you a series of questions about what type of application you are building and what features and functionality you need. It uses this information to create a shell of an application that you can immediately compile and run. This shell provides you with the basic infrastructure that you need to build your application around. You will see how this works as you follow these steps:
FIGURE 1.7. The New Project Information screen.
FIGURE 1.8. Your workspace with a tree view of the project's classes.
FIGURE 1.9. The Output pane displays any compiler errors.
FIGURE 1.10. The unmodified application shell.
Now that you have a running application shell, you need to turn your focus to the window layout of your application. Even though the main dialog window may already be available for painting in the editor area, you should still navigate to find the dialog window in the workspace so that you can easily find the window in subsequent develop-ment efforts. To redesign the layout of your application dialog, follow these steps:
FIGURE 1.11. The Resource View tab in the workspace pane.
FIGURE 1.12. Positioning the Cancel button.
FIGURE 1.13. Right-clicking the mouse to open a pop-up menu.
FIGURE 1.14. The Cancel button properties dialog.
FIGURE 1.15. Positioning the OK button.
FIGURE 1.16. Running your redesigned application.
NOTE: If you play with your application, you will notice that the Close button still closes the application. However, the Hello button no longer does anything because you changed the ID of the button. MFC applications contain a series of macros in the source code that determine which functions to call based on the ID and event message of each control in the application. Because you changed the ID of the Hello button, these macros no longer know which function to call when the button is clicked.
FIGURE 1.17. The Class Wizard.
FIGURE 1.18. The Class Wizard Add Member Function dialog.
FIGURE 1.19. The list of available functions in the Class Wizard.
FIGURE 1.20. Source code view where you insert Listing 1.1.
1: Void CHelloDlg::OnHello() 2: { 3: // TODO: Add your control notification handler code here 4: 5: /////////////////////// 6: // MY CODE STARTS HERE 7: /////////////////////// 8: 9: // Say hello to the user 10: MessageBox("Hello. This is my first Visual C++ Application!"); 11: 12: /////////////////////// 13: // MY CODE ENDS HERE 14: ///////////////////////
15: }
FIGURE 1.21. Now your application will say hello to you.
Now that your application is functionally complete, you can still add a few details to finish off the project. Those finishing touches include
If you noticed the icon in the top-left corner of your application window, you saw three blocks with the letters M, F, and C. What does MFC have to do with your application? MFC stands for Microsoft Foundation Classes. Technically, it's the C++ class library that your application is built with, but do you want to broadcast that to every user who sees your application? Most likely not. You need to edit the application icon to display an image that you do want to represent your application. Let's get busy!
FIGURE 1.22. The standard MFC icon.
FIGURE 1.23. Your own custom icon for your application.
FIGURE 1.24. Your application's About window.
NOTE: When you open an application icon in the icon designer, the icon is sized by default at 32x32. You can also select a 16x16 size icon from the drop-down list box just above where you are drawing the icon. You should draw both of these icons because there are some instances in which the large icon will be displayed and some instance in which the small icon will be shown. You will want both icons to show the same image to represent your application.
In the dialog editor, where you design your application window, you can add the minimize and maximize buttons to the title bar of your application window by following these steps:
FIGURE 1.25. Turning the minimize and maximize buttons on and off.
FIGURE 1.26. The application window with the minimize and maximize buttons.
Today you got your first taste of building applications using Visual C++. You learned about the different areas of the Visual C++ Developer Studio and what function each of these areas serves. You also learned how you can rearrange the Developer Studio environment to suit the way you work. You also learned how you can use the Visual C++ wizards to create an application shell and then attach functionality to the visual components that you place on your application windows.
// Say hello to the user MessageBox("Hello. This is my first Visual C++ Application!",
"My First Application");
The Workshop provides quiz questions to help solidify your understanding of the material covered and exercises to provide you with experience in using what you've learned. The answers to the quiz questions and exercises are provided in Appendix B, "Answers."
Add a second button to the About window in your application. Have the button display a different message from the one on the first window.
© Copyright, Macmillan Computer Publishing. All rights reserved.