Download CD Content
So far this book has shown you C++ techniques that work with any C++ compiler on any operating system. You could compile the code samples you have seen so far with Borland C++ Compiler, Microsoft Visual C++, GCC++™, and literally dozens of other compilers. You could also compile and run the code on a Windows PC, a Macintosh, a Unix server, or a Linux machine. Standard C++ code is designed to work this way—on any ISO standard C++ compiler with any operating system. C++ is not a platform- specific language. However, there do exist operating-system-specific extensions to the C++ language. Microsoft Visual C++ is one such extension. The Visual C++ development tool supports standard C++, but it also includes a lot of Windows-specific code. Microsoft Windows is a ubiquitous operating system, and its version of C++, Visual C++, is in high demand. For this reason, it seems prudent to provide you with a solid introduction to programming with Visual C++. For that reason, these last two chapters will be devoted to giving you the basics of Visual C++. This will require you to have a copy of Visual C++. If you do not, then you should feel free to skip these final chapters.
All of the standard C++ code and techniques you have learned will work just fine in Visual C++. There is nothing removed from the standard C++ language. There is, however, a great deal of Windows-specific code added. This means that you won’t be dismissing any of the skills you have worked so hard to acquire, you will simply be adding to them. It will be important to realize that the Windows-specific code will only work with Visual C++ and will not work with other C++ compilers. This is because it is not ISO standard C++. That is an important distinction to keep in mind. Visual C++ includes a lot of code that is not ISO standard C++ that will not work on other C++ compilers, or on other operating systems.
Most people have, at least occasionally, used the Windows operating system. However, most casual users, and even many programmers, don’t understand how Windows works. The first thing to be aware of is that everything you see is a window. The button on a toolbar is a window, the toolbar itself is a window, and the application that the toolbar is in is a window. Each separate component is treated as its own window. Each of these windows must communicate with each other and with the underlying operating system. This communication is handled via messages. If you click on any part of any window, a mouse click message is sent. If you want to do something when such a message is sent, then you will write some function to be executed when that event is received.
These messages are going back and forth all the time. Windows has a queue (remember queues?) that the messages are added to. The messages are then processed out and either handled by functions written to respond to specific messages, or simply discarded if there is not handler function assigned. The most important thing for you to remember right now is that everything occurs via these messages.
The second important thing for you to be aware of in the Windows architecture, is the Microsoft Foundation Classes (MFC). The MFC is a large set of classes built into Windows. These classes are designed to handle just about anything you might wish to do. There are classes to make dialog boxes, handle graphics and colors, make buttons, and so on. Visual C++ is based on the principle of automating much of the use of these foundation classes for you. As we explore Visual C++, you will get exposed to many different classes found in the Microsoft Solutions Framework.
All standard C++ programs have a main function that returns an int. Things are a little bit different with Windows programs. Every 32-bit Windows application has two primary functions you need to be concerned with: WinMain and WndProc. Your program will have one WinMain for the entire program, and one WndProc for each window in the program. WinMain is the function that starts your application. Once your application is running, the Windows operating system will start placing your applications messages in a message queue. WinMain makes three Windows Application Programming Interface (API) calls to get these messages from the operating system and to process them. The Windows API is simply a large group of functions that are built into the Windows operating system. MFC is simply a set of over 200 classes that are wrappers around these API functions.
The messages move from the operating system to your program in a specific order. First, the operating system calls GetMessage to retrieve a message. Then, the operating system calls TranslateMessage to perform any necessary conversion of the message. Finally, WinMain calls DispatchMessage, which tells the operating system to send the message to the appropriate WndProc for handling.