Setting the Pen or Brush Color
The following example shows how an application can change the DC pen color by using the GetStockObject function or SetDCPenColor and the SetDCBrushColor functions.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// Initializing original object
HGDIOBJ original = NULL;
// Saving the original object
original = SelectObject(hdc,GetStockObject(DC_PEN));
// Rectangle function is defined as...
// BOOL Rectangle(hdc, xLeft, yTop, yRight, yBottom);
// Drawing a rectangle with just a black pen
// The black pen object is selected and sent to the current device context
// The default brush is WHITE_BRUSH
SelectObject(hdc, GetStockObject(BLACK_PEN));
Rectangle(hdc,0,0,200,200);
// Select DC_PEN so you can change the color of the pen with
// COLORREF SetDCPenColor(HDC hdc, COLORREF color)
SelectObject(hdc, GetStockObject(DC_PEN));
// Select DC_BRUSH so you can change the brush color from the
// default WHITE_BRUSH to any other color
SelectObject(hdc, GetStockObject(DC_BRUSH));
// Set the DC Brush to Red
// The RGB macro is declared in "Windowsx.h"
SetDCBrushColor(hdc, RGB(255,0,0));
// Set the Pen to Blue
SetDCPenColor(hdc, RGB(0,0,255));
// Drawing a rectangle with the current Device Context
Rectangle(hdc,100,300,200,400);
// Changing the color of the brush to Green
SetDCBrushColor(hdc, RGB(0,255,0));
Rectangle(hdc,300,150,500,300);
// Restoring the original object
SelectObject(hdc,original);
// It is not necessary to call DeleteObject to delete stock objects.
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}