Handling Paint Cycles, Drawing Stages, and Notification Messages

Common controls paint and erase themselves based on messages received from the OS or other applications. The process of a control painting or erasing itself is called a paint cycle. Controls that support custom draw send NM_CUSTOMDRAW notification messages periodically throughout each paint cycle. This notification message is accompanied by an NMCUSTOMDRAW structure or another structure that contains an NMCUSTOMDRAW structure as its first member.

In addition to other data, the NMCUSTOMDRAW structure informs the parent window about what stage of the paint cycle the control is in. This is referred to as the draw stage and is represented by the value in the structure's dwDrawStage member. A control informs its parent about four basic, or global, draw stages. The following table shows the flag values, defined in the Commctrl.h header file, that represent these stages in the structure.

Global draw stage value
Description
CDDS_PREPAINT Before the paint cycle begins
CDDS_POSTPAINT After the paint cycle is complete
CDDS_PREERASE Before the erase cycle begins
CDDS_POSTERASE After the erase cycle is complete

Each of the preceding values can be combined with the CDDS_ITEM flag to specify draw stages for items. The following table shows item-specific values contained in the Commctrl.h header file.

Item-specific draw stage value
Description
CDDS_ITEMPREPAINT Before an item is drawn
CDDS_ITEMPOSTPAINT After an item has been drawn
CDDS_ITEMPREERASE Before an item is erased
CDDS_ITEMPOSTERASE After an item has been erased

You must process the NM_CUSTOMDRAW notification message and then return a specific value that informs the control what it must do.

The key to harnessing custom draw features is in responding to the NM_CUSTOMDRAW notification messages that a control sends. The return values your application sends in response to these notifications determine the control behavior for that paint cycle.