Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
COLOR_ACTIVEBORDER,COLOR_ACTIVECAPTION,
COLOR_APPWORKSPACE, COLOR_BACKGROUND, COLOR_BTNFACE,
COLOR_BTNSHADOW, COLOR_BTNTEXT, COLOR_CAPTIONTEXT,
COLOR_GRAYTEXT, COLOR_HIGHLIGHT, COLOR_HIGHLIGHTTEXT,
COLOR_INACTIVEBORDER, COLOR_INACTIVECAPTION, COLOR_MENU,
COLOR_MENUTEXT, COLOR_SCROLLBAR, COLOR_WINDOW,
COLOR_WINDOWFRAME, and COLOR_WINDOWTEXT.
To use one of these colors, cast it to HBRUSH and add 1 to its constant to paint the
background of your window:
CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
const char *StrWndName = "Windows Fundamentals";
WndCls.style = CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = AfxWndProc;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = AfxGetInstanceHandle();
WndCls.hIcon = AfxGetApp()->LoadStandardIcon(IDI_WARNING);
WndCls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
WndCls.hbrBackground = (HBRUSH)(COLOR_ACTIVECAPTION+1);
WndCls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
}
To get the value of a system color, call the GetSysColor() function. Its syntax is:
DWORD GetSysColor(int nIndex);
The nIndex argument should be a valid name of one of the system color constants such as
COLOR_ACTIVECAPTION. When this function has executed, it returns the
COLORREF value of the nIndex color. If you provide a wrong or unrecognized value as
the nIndex argument, this function returns 0, which is also a color and can therefore
produce an unexpected result. If you want to consider only existing valid colors, call the
GetSysColorBrush() function instead. Its syntax is:
HBRUSH GetSysColorBrush( int nIndex);
This function returns the color value of the system color that is passed as nIndex. If the
value of nIndex is not valid, the function returns NULL, which is not 0 and therefore is
not a color, producing a more predictable result.
Practical Learning: Building a Window Class
- Change the contents of the file as follows:
#include <windows.h>
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,