Home > Articles > Programming > Windows Programming

  • Print
  • + Share This

15.2 Encapsulation

A lot of Windows APIs take a large number of arguments. For instance, when you create a window, you have to be able to specify its position, size, style, title, and so on. That's why CreateWindowEx takes 12 arguments. RegisterClassEx also requires 12 parameters, but they are combined into a single structure, WNDCLASSEX. As you can see, there is no consistency in the design of Windows API.

Our approach to encapsulating this type of API is to create a C11 class that combines all the arguments in one place. Since most of these arguments have sensible defaults, the constructor should initialize them appropriately. Those parameters that don't have natural defaults are passed as arguments to the constructor. The idea is that once an object of such a class is constructed, it should be usable without further modification. However, if modifications are desired, they can be done by calling appropriate methods. For instance, if we are to develop the class WinMaker into a more useful form, we should add methods such as SetPosition and SetSize that override the default settings for _x, _y, _width, and _height.

Let's analyze the classes WinClassMaker and WinMaker in this context. WinClassMaker encapsulates the API RegisterClassEx. The argument to this API is a structure that we can embed directly into our class. Three of the ten parameters—window procedure, class name, and program instance—cannot use the defaults so they are passed in the constructor. The window background color normally de-faults to whatever the current system setting is—that's the COLOR_WINDOW constant. The mouse cursor in most cases defaults to whatever the system considers an arrow—that's the IDC_ARROW constant. The size of the structure must be set to sizeof(WNDCLASSEX). The rest of the parameters can be safely set to zero. We don't have to do anything else before calling WinClassMaker::Register. Of course, in a more sophisticated program we might want to modify some of these settings, and we would most certainly add methods to do that. We'll talk about it later.

In a similar way, the API CreateWindowEx is encapsulated in the class WinMaker. The nondefaultable parameters are the class name, the program instance, and the title of the window. This time, however, we might want to call WinMaker::Create multiple times in order to create more than one window. Most likely these windows would have different titles, so we pass the title as an argument to Create.

To summarize, in the main procedure your program creates a window of a particular class and enters the message processing loop. In this loop, the program waits idly for a message from Windows. Once the message arrives, the program dispatches it back to Windows. The system then calls back the program's window procedure with the same message. It is either processed manually or by the default processing. The window procedure must be implemented in your program, and its address is passed to the system during window class registration. After returning from the window procedure, control goes back to the message loop and the whole process repeats itself. Eventually a "quit" message is posted and the loop ends.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.