Desktop theme system color

xiaoxiao2021-03-06  72

System color:

After finishing the desktop icon, we will introduce the system color, relatively speaking, the settings of the system color are relatively simple, because we can use the Win32API function getSyscolor () and setsyscolors () to complete our purpose.

Get the system color:

If our program needs to get the system color, you have to call the getSysColor () this API function, incompose color parameters, the return value of this function is the color value we want, and its definition is as follows:

DWORD GETSYSCOLOR

INT NINDEX / / color parameter, such as: Color_ActiveCaption, Color_AppWorkspace, etc.

);

GetSysColor () did completing the acquisition of the system color, but we often use TColor variables or VCL color constants in practical applications, and sometimes mixed with the API function, then understanding their previous transformation is especially important.

Transform the color value obtained by GetSysColor function into a VCL color property

Canvas-> Pen-> Color = (tcolor) getsyscolor (color_activecaption);

Transforming the VCL color attribute to the color parameters that the API function can use

SetTextColor (HDC, ColorTorgb (Claqua));

The COLORTORGB function is a function in C Builder. Its function is to convert the VCL color to the color value that the Win32API function can use, and the setTextColor is the API function, its role is to set the text color on the specified device.

Set system color

Setting the system color is not as simple as getting the system color, as long as it passes its color parameters, you can return the color we need, and the system color is done through the setsyscolors function, and its purpose is to set the color of the system element, because Setting the system color is a relatively time-consuming job, so batch settings can reduce the system burden, which is because this reason does not exist that the GetSysColor's setsyscolor function is used to set the specified system color, but replace it with the setsyscolors function. It is defined as follows:

Bool setsyscolors

INT CELEMENTS,

Const Int * Lpalements,

Const colorref * lpargbvalues

);

The meaning of the parameters:

iCelements expresses the number of system colors

Const int * LPAELEMENTS, system color parameter array, its size is specified by the Icelements parameter

Const ColorRef * lpargbvalues ​​wants to set the color array of system colors, and its size is specified by the Icelements parameter.

If the system color setting is successful, the function returns true, otherwise returns false

Below I use the program code that implements that software, the code is as follows:

// Set the system color, the number of colors will not exceed 100

Int savecolorno [100];

ColorRef savecolors [100];

For (int i = 0; i <= tmycolor_max; i = i 1) {

Savecolorno [I] = TMYCOLOM [I];

Savecolors [i] = fmycolors [i];

}

SetsysColors ((int) TMYCOLOR_MAX, SAVECOLOMNO, SAVECOLORS); / / Settings System Colors

The TMYCOLORNO array and the FMYCOLORS array used in the code are used to store color constants and actual color values. Since the setsyscolors function is called, the color value in the FMYCOLORS array is to be used to the ColorRef type, these arrays and variables define you You can refer to the definition in the MSDN or the source program. Store color values:

Call the SetSysColors function to complete the system color setting, but at this time, the color settings are only valid for the current system. If the user restarts the computer, it will restore the previous color, if you want to make the color value of the change can keep down, then you You must write the system color you set in the form of the three primary color components, and the user will load the color you set, and the system color is stored under HKEY_CURRENT_USER / CONTROL Panel / Colors, know the system color is stored in registration. After the location, now we have to do just how to convert the existing color into the form of RGB color components, then write into the registry. The following code completes the write operation of the system color:

For (INT i = 0; i <= tmycolor_max; i )

{

SetKeyValue ("ControlPanel // Colors", Ansistring (TMYCOLORREG [I]), ColORTORGBSTRING (FMYCOLORS [I]));

}

The above code calls my custom function to complete the conversion of the color into a string, which is defined as follows:

Ansistring Colorgbstring (Tcolor Color)

{

TVARREC AddrColor [] =

{

Getrvalue (Color), GetGvalue (Color), GetBValue (Color)

}

RETURN FORMAT ("% D% D% D", AddrColor, 2);

}

The TVARREC variable is a variable type of the unfained type in C Builder. It is mainly used for the parameters of the function, as an array of unfained types, if you don't know the specific type of variable, you can use this Type variable. GetRvalue (), getGvalue (), get the color component value of R, g, b of the TCOLOR color type when getBValue (). Ok, or like the settings of the desktop icon, I still use an example program as the end of this section, the code of the sample program is changed, the code is as follows:

Unit.h files are as follows:

#ifndef unit1h

#define unit1h

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#include

#include

#include

/ / -------------------------------------------------------------------------------------------- ---------------------------

Class TFORM1: Public TForm

{

__published: // Ide-management Components

TButton * btnchangecolor;

TButton * btnResumecolor;

Void __fastcall btnchangecolorclick (TOBJECT * Sender);

Void __fastcall btnreSuMecolorClick (TOBJECT * Sender); private: // user declarations

Tcolor Oldcolors [2];

Int Colorno [2];

ColorRef colors [2];

Public: // user declarations

__fastcall tform1 (tComponent * Owner);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Extern package tform1 * form1;

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ENDIF

Unit1.cpp files are as follows:

#include

#pragma HDRSTOP

#include "unit1.h"

/ / -------------------------------------------------------------------------------------------- ---------------------------

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

/ / Save the current color

OldColors [0] = (tcolor) getsyscolor (color_windowtext);

OldColors [1] = (TColor) getsyscolor (color_window);

Colorno [0] = Color_Window;

Colorno [1] = Color_WindowText;

Colors [0] = RGB (255, 0, 125);

Colorno [1] = RGB (125, 0, 255);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: btnchangecolorclick (Tobject * Sender)

{

/ / Change the system color

Setsyscolors (2, Colorno, Colors);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: btnreSumecolorClick (Tobject * Sender)

{

/ / Change the system color

ColorRef Savecolors [2] = {Oldcolors [0], Oldcolors [1]};

Setsyscolors (2, Colorno, Savecolors);

}

转载请注明原文地址:https://www.9cbs.com/read-108775.html

New Post(0)