Monday 28 November 2011

How do you access the system colors in a Windows program? in C programming

How do you access the system colors in a Windows program?

You can obtain the system colors by calling the Windows API function GetSysColor(). The GetSysColor() function takes one parameter, which signifies which color element you want to obtain. The color elements are represented by color constants defined in the windows.h header file. The Windows system color constants are listed in the following FAQ (XXI.25). 

For instance, to obtain the color for the window’s active border, you might make the following function call:

rgbColor = GetSysColor(COLOR_ACTIVEBORDER

The GetSysColor() function returns an RGB value. The RGB value represents the intensity of the colors red, green, and blue that are present in the returned color. An RGB value of 0 signifies black, and an RGB value of 255 signifies white. You can extract the individual red, green, and blue values from the RGB value by calling the GetRValue(), GetGValue(), and GetBValue() Windows API functions.

The Windows API function SetSysColors() can be used to set system colors. Here is an example of some
code that sets the color of the active border to red:

int aiColorElements[1];
DWORD argbColor[1];
aiColorElements[0] = COLOR_ACTIVEBORDER;
argbColor[0] = RGB(0xFF, 0x00, 0x00);
SetSysColors(1, aiColorElements, argbColor);

The SetSysColors() function takes three arguments. The first argument is the number of elements to set color for, the second is an array of integers holding the system color constants to set color for, and the third is an array of RGB values that correspond to the colors you want to invoke for the elements represented by the second argument.

Cross Reference:

XXI.25: What are the system color constants?

No comments:

Post a Comment