Article ID: 119736
Article Last Modified on 10/30/2003
VBM_SETPROPERTY - so you can determine when the backcolor changes.
WM_ERASEBKGND - so you can repaint the background.
WM_PAINT - BeginPaint sends the WM_ERASEBKGND message.
// Check to see whether BackColor has been set.
case VBM_SETPROPERTY:
if( wp == IPROP_BACKCOLOR )
InvalidateRect(hwnd, NULL, TRUE); // Force a repaint.
break;
// Paint the background.
case WM_ERASEBKGND:
{
HBRUSH hbr, hbrOld=NULL;
RECT rect;
// Get window coordinates, and normalize.
GetWindowRect(hwnd, &rect);
rect.right = rect.right - rect.left; // Get width.
rect.bottom = rect.bottom - rect.top; // Get height.
rect.left = rect.top = 0;
// Get a brush with the background color.
hbr = (HBRUSH)SendMessage(GetParent(hwnd), WM_CTLCOLOR, (HDC)wp,
MAKELONG(hwnd, 0));
// If we got a brush, select it into our DC.
if( hbr )
hbrOld = SelectObject((HDC)wp, hbr);
// Paint the background.
FillRect((HDC)wp, &rect, hbr);
// If we selected an object, restore old object.
if( hbrOld )
SelectObject((HDC)wp, hbrOld);
// Return a non-zero, indicating we processed a message.
return 1;
}
// Paint the control (assumes we have a PaintRoutine).
case WM_PAINT:
if( wp ) PaintRoutine(hwnd, (HDC) wp); // Paint to given HDC.
else // Paint to window's HDC.
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
PaintRoutine(hwnd, ps.hdc);
EndPaint(hwnd, &ps);
}
break;
void PaintRoutine(HWND hwnd, HDC hdc)
{
RECT rect;
char msg[] = "Painting the control";
// TextOut will work with any DC.
TextOut(hdc, 0, 0, msg, sizeof(msg) - 1);
}
Additional query words: 3.00
Keywords: KB119736