Common Control Basics

The new common controls were designed specifically to enhance the look and feel of new interface elements such as those seen in Windows Explorer. If you as a developer want to do everything you can to make your applications look and feel as if they are part of the new interface, you need to understand how these controls work and how to integrate them into your applications.

When I started running Microsoft Windows 95, I was delighted to learn about the new controls that were available to me through the common control library. Some of these, such as property sheets, wizards, and tree view controls, provided just the type of functionality I was looking for in order to add some polish to my applications without too much effort. Now I could finally add wizards to my programs without having to write my own code to manage the windows! I discovered other controls, too: progress bars, trackbars, up-down controls, and animation controls, to name just a few.

In Part I of this book, we'll examine each new common control in turn, and I'll provide sample code in C and in C++ using the Microsoft Foundation Class Library (MFC) to help you understand how to create and manipulate the controls. I have also included lists of messages and member functions to show you the breadth of functionality each control supports. (These lists are based in large part on the documentation for the Win32 Software Development Kit [SDK] and the documentation for MFC version 3.1, but the lists are not comprehensive. If you would like even more detailed information about the common controls, you can also refer to the Win32 SDK itself, consult the MFC documentation, or check out the Microsoft Developer Network [MSDN] Development Library.) But first let's take a look at what all the “common” controls have in common.

Most of the new 32-bit common controls designed for the Windows 95 operating system are supported by the COMCTL32.DLL dynamic-link library (DLL), which is supported by the 32-bit Microsoft Windows operating systems: Windows 95, Win32s version 1.3 or later (running on Windows version 3.1), and Windows NT version 3.51 or later. It is important to remember that these new controls are 32-bit only—they are not supported in 16-bit Windows environments. If I've confused you with that last statement, let me clarify: these controls will work in Windows 3.1, but only if the system is running Win32s, which provides a mechanism called a thunking layer (don't you love that name?) to translate between 32-bit system calls and 16-bit system calls.

Essentially, a common control is a child window that sends a notification to its parent window when an event (such as a mouse click or a focus change) occurs in the control. Because these controls are windows, you can use the standard window management functions to manipulate them—that is, you can send messages or post messages to them. Some common controls send notifications as WM_COMMAND messages; others use a new message, WM_NOTIFY, to notify the control's parent of an action or a change. Each common control supports a set of messages that an application can use to manipulate the control.

Instead of using the SendMessage or PostMessage function to send messages to some of the common controls, an application can use a set of macros included in the COMMCTRL.H header file. I'm more comfortable using these macros than using the standard functions or the standard messages because the preface of the macro name specifies the type of control you are manipulating. For example, the ListView_DeleteColumn macro deletes a column in a list view control. When I ported my samples from C to MFC, I was able to easily strip off the preface of the macro and use the remainder in my calls to the member functions. In the example I just mentioned, an MFC application that has a list view control (CListCtrl class) defined as m_List would contain a member function called m_List.DeleteColumn.

The COMCTL32.DLL file contains the window procedures, resources, and functions that support common controls. Applications using the new common controls must link with the COMCTL32.LIB file. Before you make any calls into this library, you should call the Windows InitCommonControls function to ensure that this DLL has been loaded. InitCommonControls is a stub function that does nothing; it takes no parameters and returns no values. Calling this function simply confirms that the common control library has been loaded.

Each common control belongs to a window class defined by the common control library. An application creates a common control of a particular type by specifying the appropriate window class name in the CreateWindow or CreateWindowEx function, by using the Create member function for the MFC class designed to support the window class, or by using a dialog template. Table 1-1 lists the window classes provided by the common control library. You can find these definitions in the COMMCTRL.H file for C projects; for MFC projects, the AFXCMN.H file should be included in STDAFX.H.

Class Name Description
ANIMATE_CLASS Provides a method for displaying animated controls within a window.
HOTKEY_CLASS Allows the developer to define hot keys, which are key combinations that the user can press to perform an action quickly. For example, a user can press the hot key Ctrl-Z to activate a given window and bring it to the top of the z-order. The hot-key control displays the user's choices and ensures that the user selects a valid key combination.

Table 1-1. (continued)

Common control window classes.

Class Name Description
PROGRESS_CLASS Provides a method for indicating the progress of a lengthy operation by gradually filling a rectangle from left to right with the system highlight color as the operation progresses.
STATUSCLASSNAME Provides a method for displaying status information.
TOOLBARCLASSNAME Provides buttons that carry out menu commands.
TOOLTIPS_CLASS Creates a ToolTip control, which displays a small pop-up window containing text that explains the purpose of a tool in an application. ToolTips are generally used with toolbars.
TRACKBAR_CLASS Allows the user to select from a range of values by moving a slider.
UPDOWN_CLASS Provides a pair of arrows to increment or decrement the value in an adjacent (buddy) control.
WC_HEADER Provides a method for displaying a header above a column of information and allows the user to sort the information by clicking the header.
WC_LISTVIEW Provides a method for displaying and arranging a collection of items. Each item consists of an icon and a label.
WC_TABCONTROL Provides a method for defining multiple pages for the same area of a window or a dialog box. Each page contains specific information or a group of controls that the application displays when the user clicks the corresponding tab.
WC_TREEVIEW Provides a method for displaying a hierarchical list of items. Each item consists of a label and, optionally, a bitmap.