Adding Palette Message Handlers

[The feature associated with this page, DrawDib, is a legacy feature. It has been superseded by MediaComposition class. MediaComposition class has been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaComposition class instead of DrawDib, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

The following example illustrates simple message handlers for the WM_PALETTECHANGED and WM_QUERYNEWPALETTE messages. The example uses the DrawDibRealize function to process the WM_QUERYNEWPALETTE message.

Your application should respond to the WM_QUERYNEWPALETTE message by invalidating the destination window to let the DrawDibDraw function redraw an image. You should respond to the WM_PALETTECHANGED message by using the DrawDibRealize function to realize the palette.

case WM_PALETTECHANGED: 
    if ((HWND)wParam == hwnd) 
        break; 
case WM_QUERYNEWPALETTE: 
    hdc = GetDC(hwnd); 
    f = DrawDibRealize(hdd, hdc, FALSE) > 0; 
    ReleaseDC(hwnd, hdc); 
    if (f) 
        InvalidateRect(hwnd, NULL, TRUE); 
    break; 

Using DrawDib