Use the direct method to specify a palette color by supplying an index into your logical palette instead of an explicit RGB value to functions that expect a color. The PALETTEINDEX macro accepts an integer representing an index into your logical palette and returns a palette-index COLORREF value, which you would use as the color specifier for such functions. For example, to fill a region bounded by pure green with a solid brush consisting of pure red, you could use a sequence similar to the following:
pLogPal->palPalEntry[5].pRed = 0xFF;
pLogPal->palPalEntry[5].pGreen = 0x00;
pLogPal->palPalEntry[5].pBlue = 0x00;
pLogPal->palPalEntry[5].pFlags = (BYTE) 0;
pLogPal->palPalEntry[6].pRed = 0x00;
pLogPal->palPalEntry[6].pGreen = 0xFF;
pLogPal->palPalEntry[6].pBlue = 0x00;
pLogPal->palPalEntry[6].pFlags = (BYTE) 0;
.
.
.
hPal = CreatePalette((LPSTR) pLogPal);
hDC = GetDC(hWnd);
SelectPalette(hDC, hPal, 0);
RealizePalette(hDC);
lSolidBrushColor = PALETTEINDEX(5);
lBoundaryColor = PALETTEINDEX(6);
hSolidBrush = CreateSolidBrush(lSolidBrushColor);
hOldSolidBrush = SelectObject(hDC, hSolidBrush);
hPen = CreatePen(lBoundaryColor);
hOldPen = SelectObject(hDC, hPen);
Rectangle(hDC, x1, y1, x2, y2);
This example indicates to Windows that it should draw a rectangle bounded by the color in the palette entry at index 6 (green) and filled with the color located in the entry at index 5 (red).
Note that the brush created by CreateSolidBrush is independent of any device context. As a result, the color specified in the lSolidBrushColor parameter is the color in the sixth entry of the palette that was current when the brush was selected into the device context (not when the application created the brush). Selecting and realizing a different palette and selecting the brush again would change the color drawn by the brush. Thus, when using a logical palette, you need only create a brush for each type required (such as solid or vertical hatch). You can then change the color of the brush by using different palettes or by changing the color in the palette entry to which the brush refers.