Function Relative message Functionality Calling order
OnNCPaint WM_NCPAINT Paint nonclient area(window frame by default) 0
OnEraseBkgnd WM_ERASEBKGND Erase a window background 1
OnPaint WM_PAINT Repaint a window rect(usually the client area) 2
DrawItem WM_DRAWITEM In place of OnPaint draws an owner-draw control 2
OnCtlColor WM_CTLCOLOR Change a control’s color(in parent) 3
CtlColor WM_CTLCOLOR Change a control’s color(in child) 3
Note:
1. OnPaint cannot coexist with DrawItem,when you put them together the latter is ignored.
2. We can change a control’s text by using SetFont,but has no direct way of altering its color.And that’s how WM_CTLCOLOR comes to the rescue.Windows supplies two ways to modify a child control,and both ways request the child to send its parent
a message(WM_CTLCOLOR) which contains its DC information.Then based on the parent’s reaction,we got our two ways:
A. The parent responds to the message in OnCtlColor,then calls CDC::SetTextColor or CDC::SetBkColor to change control text font attributes.Finally it must return a HBRUSH which is to be used for painting the control background.
B. The parent can choose not to handle the message but return it to the child by using a special macro in MFC which goes like ON_WM_CTLCOLOR_REFLECT,then the child can do some changes in CtlColor responding to the message.
3. Both OnCtlColor and CtlColor are called when a child control is about to be drawn.