Since hard-coding a color bitmap may require considerable effort, it is usually simpler to create a compatible bitmap and draw in it. For example, to create a color bitmap that has a red, green, and blue plaid pattern, you simply create a blank bitmap and use the PatBlt function, with the red, green, and blue brushes, to draw the pattern. This method has the advantage of generating a reasonable bitmap even if the screen does not support color. This is because GDI provides dithered brushes for monochrome screens when a color brush is requested. A dithered brush is a unique pattern of pixels that represents a color when that color is not available for the device.
The following statements create the color bitmap by drawing it:
#define PATORDEST 0x00FA0089L
HDC hdc;
HDC hdcMemory;
HBITMAP hBitmap;
HBITMAP hOldBitmap;
HBRUSH hRedBrush;
HBRUSH hGreenBrush;
HBRUSH hBlueBrush;
HBRUSH hOldBrush;
.
.
.
hdc = GetDC(hwnd);
if ((hdcMemory = CreateCompatibleDC(hdc)) == NULL)
return NULL;
if ((hBitmap = CreateCompatibleBitmap(hdc, 64, 32)) == NULL)
return NULL;
hOldBitmap = SelectObject(hdcMemory, hBitmap);
hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
hGreenBrush = CreateSolidBrush(RGB(0, 255, 0));
hBlueBrush = CreateSolidBrush(RGB(0, 0, 255));
PatBlt(hdcMemory, 0, 0, 64, 32, BLACKNESS);
hOldBrush = SelectObject(hdcMemory, hRedBrush);
PatBlt(hdcMemory, 0, 0, 24, 11, PATORDEST);
PatBlt(hdcMemory, 40, 10, 24, 12, PATORDEST);
PatBlt(hdcMemory, 20, 21, 24, 11, PATORDEST);
SelectObject(hdcMemory, hGreenBrush);
PatBlt(hdcMemory, 20, 0, 24, 11, PATORDEST);
PatBlt(hdcMemory, 0, 10, 24, 12, PATORDEST);
PatBlt(hdcMemory, 40, 21, 24, 11, PATORDEST);
SelectObject(hdcMemory, hBlueBrush);
PatBlt(hdcMemory, 40, 0, 24, 11, PATORDEST);
PatBlt(hdcMemory, 20, 10, 24, 12, PATORDEST);
PatBlt(hdcMemory, 0, 21, 24, 11, PATORDEST);
BitBlt(hdc, 0, 0, 64, 32, hdcMemory, 0, 0, SRCCOPY)
SelectObject(hdcMemory, hOldBrush);
DeleteObject(hRedBrush);
DeleteObject(hGreenBrush);
DeleteObject(hBlueBrush);
SelectObject(hdcMemory, hOldBitmap);
ReleaseDC(hwnd, hdc);
DeleteDC(hdcMemory);
In this example, the CreateSolidBrush function creates the red, green, and blue brushes needed to make the plaid pattern. The SelectObject function selects each brush into the memory device context as that brush is needed, and the PatBlt function paints the colors into the bitmap. Each color is painted three times, each time into a small rectangle. In this example, the application instructs PatBlt to overlap the different color rectangles slightly. Since the PATORDEST raster-operation code is specified, PatBlt uses a Boolean OR operator to combine the brush color with the color already in the bitmap. The result is a different color border around each rectangle. After the bitmap is complete, BitBlt copies it from the memory device context to the screen.