INF: Processing WM_PALETTECHANGED and WM_QUERYNEWPALETTE

ID Number: Q77702

3.00

WINDOWS

Summary:

An application that manipulates the system palette should process the

WM_PALETTECHANGED and WM_QUERYNEWPALETTE messages to maintain its

appearance during system palette and input focus changes.

More Information:

The WM_PALETTECHANGED message informs all windows that the window with

input focus has realized its logical palette, thereby changing the

system palette. This message allows a window without input focus that

uses a color palette to realize its logical palettes and update its

client area.

This message is sent to all windows, including the one that changed

the system palette and caused this message to be sent. The wParam of

this message contains the handle of the window that caused the system

palette to change. To avoid an infinite loop, care must be taken to

check that the wParam of this message does not match the window's

handle. The following sample code demonstrates how to process

WM_PALETTECHANGED:

case WM_PALETTECHANGED:

{

HDC hDC; // Handle to device context

HPALETTE hOldPal; // Handle to previous logical palette

// If this application did not change the palette, select

// and realize this application's palette

if (wParam != hWnd)

{

// Need the window's DC for SelectPalette/RealizePalette

hDC = GetDC(hWnd);

// Select and realize hPalette

hOldPal = SelectPalette(hDC, hPalette, FALSE);

RealizePalette(hDC);

// When updating the colors for an inactive window,

// UpdateColors can be called because it is faster than

// redrawing the client area (even though the results are

// not as good)

UpdateColors(hDC);

// Clean up

if (hOldPal)

SelectPalette(hDC, hOldPal, FALSE);

ReleaseDC(hWnd, hDC);

}

}

break;

Note: The WM_PALETTECHANGED message is sent to all top-level and

overlapped windows; therefore, if any child window uses a color

palette, this message must be passed on to it.

The WM_QUERYNEWPALETTE message informs a window that it is about to

receive input focus. In response, the window receiving focus should

realize its palette as a foreground palette and update its client

area. If the window realizes its palette, it should return TRUE;

otherwise, it should return FALSE. The following sample code

demonstrates processing WM_QUERYNEWPALETTE:

case WM_QUERYNEWPALETTE:

{

HDC hDC; // Handle to device context

HPALETTE hOldPal; // Handle to previous logical palette

// Need the window's DC for SelectPalette/RealizePalette

hDC = GetDC(hWnd);

// Select and realize hPalette

hOldPal = SelectPalette(hDC, hPalette, FALSE);

RealizePalette(hDC);

// Redraw the entire client area

InvalidateRect(hWnd, NULL, TRUE);

UpdateWindow(hWnd);

// Clean up

if (hOldPal)

SelectPalette(hDC, hOldPal, FALSE);

ReleaseDC(hWnd, hDC);

// Message processed, return TRUE

return TRUE;

}

Note: The WM_QUERYNEWPALETTE message is sent to all top-level and

overlapped windows; therefore, if any child window uses a color

palette, this message must be passed on to it.