Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 8 GDI Orientation and Transformations Visual C++ and MFC Fundamentals


COLORREF NewColor;
}

When of after declaring such a variable, you can initialize it with a 32-bit decimal value.
Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
COLORREF NewColor = 16711935;
}

Although the above number (16711935) is a legitimate color value, it does not mean
much. To create a color value, the Win32 API provides the RGB macro. Its syntax is:

COLORREF RGB(BYTE byRed, BYTE byGreen, BYTE byBlue);

The RGB macro behaves like a function and allows you to pass three numeric values
separated by a comma. Each value must be between 0 and 255. Therefore, the above
initialization can be done as follows:

void CExoView::OnDraw(CDC* pDC)
{
COLORREF NewColor = RGB(255, 0, 255);
}

Whether a color was initialized by a 32-bit integer or using the RGB macro, if you want
to retrieve the red, green, and blue values of a color, you can use the GetRValue(), the
GetGValue(), of the GetBValue() macros to extract the value of each. The syntaxes of
these macros are:

BYTE GetRValue(DWORD rgb);
BYTE GetGValue(DWORD rgb);
BYTE GetBValue(DWORD rgb);

Each macro takes a 32-bit value as argument, arg. The GetRValue() macro returns the red
value of the rgb number. The GetGValue() macro returns the green value of the rgb
number. The GetBValue() macro returns the blue value of the rgb number.

7.1.3 Color Palettes........................................................................................


Device independence is the ability for an application to draw its intended figures, text,
shapes, and display colors regardless of the device on which the drawing is performed.
One way to take care of this is to manage colors at the operating system level so that
Microsoft Windows can select the right color to render an object or portion of it. In some
cases, a device, such as a monitor or a printer, may need to take care of the coloring
details of the jobs it is asked to perform.

A color palette is a list of colors that a device can display. For example, one device may
be able to handle only two colors; such is the case for a black and white printer. Another
device could be able to use more colors than that. To control this situation, Microsoft
Windows keeps track of the color palette of each device installed on the computer.

There are two types of color palettes. The default color palette is a list of colors that the
operating system would use on a device unless notified otherwise. There are typically 20
Free download pdf