11.4 Adding Color to a Monochrome Bitmap

If your computer has a color screen, 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 sets the foreground color to red and the background color to green:

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

In this example, the hDC variable contains the handle of the device context. The SetTextColor function sets the foreground color to red, and the SetBkColor function sets the background color to green. The RGB function 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, green, and blue by combining the color intensities. For example, the following statement creates a yellow RGB value:

RGB(255, 255, 0)

Once you have set the foreground and background colors, 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.