A header control is a window that is usually positioned above columns of text or numbers. It contains a title for each column, and it can be divided into parts. The user can drag the dividers that separate the parts to set the width of each column. The following illustration shows a header control that has labeled columns that give detailed information about files in a directory.

You can create a header control by using the
Typically, you must set the size and position of a header control to fit
within the boundaries of a particular rectangle, such as the client area of a
window. By using the
When sending HDM_LAYOUT, you specify the address of an
If you want to use HDM_LAYOUT to set the initial size and position of
a header control, set the initial visibility state of the control so that it is
hidden. After sending HDM_LAYOUT to retrieve the size and position
values, you can use the
A header control typically has several header items that define the columns
of the control. You add an item to a header control by sending the
The fmt member of an item's HDITEM structure can include either the HDF_STRING or HDF_BITMAP flag to indicate whether the control displays the item's string or bitmap. If you want to display both a string and a bitmap, create an owner-drawn item by setting the fmt member to include the HDF_OWNERDRAW flag. The HDITEM structure also specifies formatting flags that tell the control whether to center, left-align, or right-align the string or bitmap in the item's rectangle.
HDM_INSERTITEM returns the index of the newly added item. You can use
the index in other messages to set properties or retrieve information about the
item. You can delete an item by using the
You can use the
You can define individual items of a header control to be owner-drawn items. Using this technique gives you more control than you would otherwise have over the appearance of a header item.
You can use the HDM_INSERTITEM message to insert a new owner-drawn item into a header control or the HDM_SETITEM message to change an existing item to an owner-drawn item. Both messages include the address of an HDITEM structure, which should have the fmt member set to the HDF_OWNERDRAW value.
When a header control must draw an owner-drawn item, it sends the
| Member | Description |
|---|---|
| CtlType | ODT_HEADER owner-drawn control type. |
| CtlID | Child-window identifier of the header control. |
| itemID | Index of the item to be drawn. |
| itemAction | ODA_DRAWENTIRE drawing-action flag. |
| itemState | ODS_SELECTED drawing-action flag if the cursor is on the item and the mouse button is down. Otherwise, this member is zero. |
| hwndItem | Handle to the header control. |
| hDC | Handle to the device context of the header control. |
| rcItem | Coordinates of the header item to be drawn. The coordinates are relative to the upper-left corner of the header control. |
| itemData | Application-defined 32-bit value associated with the item. |
A header control sends notification messages to its parent window when the
user clicks or double-clicks an item, when the user drags an item divider, and
when the attributes of an item change. The parent window receives the
notifications in the form of
| Notification | Description |
|---|---|
| Signals the start of divider dragging. | |
| Indicates that the user double-clicked a divider. | |
| Signals the end of divider dragging. | |
| Indicates a change in the attributes of an item. | |
| Indicates that the attributes of an item are about to change. | |
| Indicates that the user clicked an item. | |
| Indicates that the user double-clicked an item. | |
| Indicates that the user dragged a divider. |
This section describes the window messages handled by the window procedure for the WC_HEADER window class.
| Message | Processing performed |
|---|---|
| Initializes the header control. | |
| Uninitializes the header control. | |
| Fills the background of the header control using the control's current background color. | |
| Returns a combination of the DLGC_WANTTAB and DLGC_WANTARROWS values. | |
| Returns the handle to the current font, which is used by the header control to draw its text. | |
| Captures mouse input. If the mouse cursor is on a divider, the control sends the HDN_BEGINTRACK notification message and begins dragging the divider. If the cursor is on an item, the item is displayed in the pressed state. | |
| Same as the WM_LBUTTONDBLCLK message. | |
| Releases the mouse capture. If the control was tracking mouse movement, it sends the HDN_ENDTRACK notification message and redraws the header control. Otherwise, the control sends the HDN_ITEMCLICK notification message and redraws the header item that was clicked. | |
| If a divider is being dragged, the control sends the HDN_TRACK notification message and displays the item at the new position. If the left mouse button is down and the cursor is on an item, the item is displayed in the pressed state. | |
| Allocates and initializes an internal data structure. | |
| Frees the resources allocated by the header control after the header control is uninitialized. | |
| Paints the invalid region of the header control. If the wParam parameter is non-NULL, the control assumes that the value is an HDC and paints using that device context. | |
| Sets the cursor shape, depending on whether the cursor is on a divider or in a header item. | |
| Selects a new font handle into the device context for the header control. |
The following example demonstrates how to create a header control and position it along the top of the parent window's client area. The control is initially hidden. The HDM_LAYOUT message is used to calculate the size and position of the control within the parent window. The control is then repositioned and made visible.
Example
// DoCreateHeader - creates a header control that is positioned along
// the top of the parent window's client area.
// Returns the handle to the header control.
// hwndParent - handle to the parent window.
//
// Global variable
// g_hinst - handle to the application instance
extern HINSTANCE g_hinst;
HWND DoCreateHeader(HWND hwndParent)
{
HWND hwndHeader;
RECT rcParent;
HDLAYOUT hdl;
WINDOWPOS wp;
// Ensure that the common control DLL is loaded, and then create
// the header control.
InitCommonControlsEx();
if ((hwndHeader = CreateWindowEx(0, WC_HEADER, (LPCTSTR) NULL,
WS_CHILD | WS_BORDER | HDS_BUTTONS | HDS_HORZ,
0, 0, 0, 0, hwndParent, (HMENU) ID_HEADER, g_hinst,
(LPVOID) NULL)) == NULL)
return (HWND) NULL;
// Retrieve the bounding rectangle of the parent window's
// client area, and then request size and position values
// from the header control.
GetClientRect(hwndParent, &rcParent);
hdl.prc = &rcParent;
hdl.pwpos = ℘
if (!SendMessage(hwndHeader, HDM_LAYOUT, 0, (LPARAM) &hdl))
return (HWND) NULL;
// Set the size, position, and visibility of the header control.
SetWindowPos(hwndHeader, wp.hwndInsertAfter, wp.x, wp.y,
wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW);
return hwndHeader;
}
The following example demonstrates how to use the
Example
// DoInsertItem - inserts an item into a header control.
// Returns the index of the new item.
// hwndHeader - handle to the header control.
// iInsertAfter - index of the previous item.
// nWidth - width of the new item.
// lpsz - address of the item string.
int DoInsertItem(HWND hwndHeader, int iInsertAfter,
int nWidth, LPSTR lpsz)
{
HDITEM hdi;
int index;
hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
hdi.pszText = lpsz;
hdi.cxy = nWidth;
hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]);
hdi.fmt = HDF_LEFT | HDF_STRING;
index = SendMessage(hwndHeader, HDM_INSERTITEM,
(WPARAM) iInsertAfter, (LPARAM) &hdi);
return index;
}
Header controls in Microsoft® Internet Explorer support the following new features.
| Image Lists | New messages for this feature include |
| Callback Items | Currently, callback support includes header item text and images.
Setting an HDITEM structure's text to the LPSTR_TEXTCALLBACK
value or its image to the I_IMAGECALLBACK value will cause the control
to send an |
| Custom Item Ordering | The new messages that support this feature are: |
| Drag-and-Drop Manipulation | Drag-and-drop reordering of header items is now available. Implement
drag-and-drop support by including the |
| Hot Tracking | When this feature is enabled, the item under the pointer will be
highlighted. You can check whether or not hot tracking is enabled by
calling |
| Text, Bitmaps, and Images | Items can simultaneously display item text, a bitmap, and an image |