Processing WM_PALETTECHANGED and WM_QUERYNEWPALETTE

Last reviewed: August 13, 1997
Article ID: Q77702

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

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.


Additional query words: 95
Keywords : GdiPal kbgraphic kbhowto
Version : WIN: 3.0, 3.1, 4.0; NT 3.50, 3.51
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.