SelectPalette Function

HPALETTE SelectPalette(
HDC hdc,
HPALETTE hpal,
BOOL bForceBackground
);

Parameters

Parameter Description
hdc A handle to the device context.
hpal A handle to the logical palette to be selected.
bForceBackground A flag that specifies whether the palette entries are to be realized as background colors. If this parameter is TRUE, all palette entries are realized as background colors. If this parameter is FALSE, the system determines which entries are realized as background colors.

Return Value

If the function succeeds, the return value is a handle to the previous logical palette selected for the specified device context. If the function fails, the return value is NULL.

Remarks

A device context can have at most one logical palette selected at any given time. The SelectPalette function selects a specified logical palette into the device context.

When an application selects a logical palette into a device context, the system examines the palette entries. For each entry, the system checks if it is a system color and if it is already used by another application. If the entry is not a system color and is not in use, it is realized as a foreground color. Otherwise, it is realized as a background color.

The bForceBackground parameter allows an application to control how palette entries are realized. If set to TRUE, all palette entries are realized as background colors, making them available for other applications to use.

It is important to always restore the original palette by calling SelectPalette with the handle returned by a previous call to SelectPalette. This ensures that the system's palette state is maintained correctly.

Note: Applications that require extensive color control should use 24-bit color (256 colors per channel) to avoid palette-related issues.

Example

The following code snippet demonstrates how to select a logical palette into a device context and then restore the original palette:

// Assume hdc is a valid device context handle and hMyPalette is a handle to a logical palette. HPALETTE hPreviousPalette = SelectPalette(hdc, hMyPalette, FALSE); // Perform drawing operations using the selected palette. // ... // Restore the original palette. SelectPalette(hdc, hPreviousPalette, TRUE);

See Also