Adding Color to Monochrome Bitmaps

If your computer has a color display, you can add color to a monochrome bitmap by setting the foreground and background colors of the display context. The foreground and background colors specify which colors the white and black bits of the bitmap will have when displayed. You set the foreground and background colors by using the SetTextColor and SetBkColor functions. The following example shows how to set the foreground color to red and the background color to green:

SetTextColor(hDC, RGB(255,0,0));

SetBkColor(hDC, RGB(0,255,0));

The hDC variable holds the handle to the device context. The SetTextColor function sets the foreground color to red. The SetBkColor function sets the background color to green. The RGB utility creates an RGB color value by using the three specified values. Each value represents an intensity for each of the primary display colors—red, green, and blue—with the value 255 representing the highest intensity, and zero, the lowest. You can produce colors other than red and green by combining the color intensities. For example, the following statement creates a yellow RGB value:

RGB(255,255,0)

Once the foreground and background colors are set, no further action is required. You can display a bitmap (as described earlier) and Windows will automatically add the foreground and background colors. The foreground color is applied to the white bits (the bits set to 1) and the background color to the black bits (the bits set to zero). Note that the background mode, as specified by the SetBkMode function, does not apply to bitmaps. Also, the foreground and background colors do not apply to color bitmaps.

When displayed in color, the bitmap named “dog” will be red, and the background will be green.