Share via


Changing the Colors of Window Elements

The following example demonstrates the use of the GetSysColor and SetSysColors functions. First, the example uses GetSysColor to retrieve the colors of the window background and active caption and displays the red, green, blue (RGB) values in hexadecimal notation. Next, example uses SetSysColors to change the color of the window background to light gray and the active title bars to dark purple. After a 10-second delay, the example restores the previous colors for these elements using SetSysColors.

#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

void main()
{
    int aElements[2] = {COLOR_WINDOW, COLOR_ACTIVECAPTION};
    DWORD aOldColors[2];
    DWORD aNewColors[2];

    // Get the current color of the window background. 
 
    aOldColors[0] = GetSysColor(aElements[0]); 

    printf("Current window color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aOldColors[0]), 
        GetGValue(aOldColors[0]), 
        GetBValue(aOldColors[0]));

    // Get the current color of the active caption. 
 
    aOldColors[1] = GetSysColor(aElements[1]); 

    printf("Current active caption color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aOldColors[1]), 
        GetGValue(aOldColors[1]), 
        GetBValue(aOldColors[1]));

    // Define new colors for the elements

    aNewColors[0] = RGB(0x80, 0x80, 0x80);  // light gray 
    aNewColors[1] = RGB(0x80, 0x00, 0x80);  // dark purple 

    printf("\nNew window color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aNewColors[0]), 
        GetGValue(aNewColors[0]), 
        GetBValue(aNewColors[0]));

    printf("New active caption color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aNewColors[1]), 
        GetGValue(aNewColors[1]), 
        GetBValue(aNewColors[1]));

    // Set the elements defined in aElements to the colors defined
    // in aNewColors

    SetSysColors(2, aElements, aNewColors); 

    printf("\nWindow background and active border have been changed.\n");
    printf("Reverting to previous colors in 10 seconds...\n");

    Sleep(10000);    

    // Restore the elements to their original colors

    SetSysColors(2, aElements, aOldColors); 
}

 

 

Send comments about this topic to Microsoft

Build date: 3/25/2010