Mirror

How to use non-standard colors in Windows (Views: 100)


Problem/Question/Abstract:

My client wants to preserve a unique look and feel that does not depend upon the Windows color settings. For example, no matter what color the user has selected for his button color, my client wants to define the color for his application. A specific example involves SpinEdits. I'd like to define the color of the edit and the spin buttons. The easiest way for me to do this is to be able to redefine Windows colors internal to my application.

Answer:

The problem is more complicated as it seems at first glance. If your application stays on top long enough and you are pretty sure that the users aren't switching between apps at runtime, you can change the colors system wide. The sample below (quite simple, but enough to give you an idea how to do it) will help you to have the same colors on all machines, independent of the user choice. The advantage of this approach is, that you are able to use standard controls (which works fine most of the time).

In this sample I am changing 11 options. Have a look in the help files for all available options.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  tarr = array[1..11] of integer;
  TForm1 = class(TForm)
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure FormDeactivate(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ind: tarr;

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
var
  p1, p2: tarr;
begin
  p1[1] := COLOR_ACTIVEBORDER;
  p1[2] := COLOR_APPWORKSPACE;
  p1[3] := COLOR_BTNFACE;
  p1[4] := COLOR_BTNHILIGHT;
  p1[5] := COLOR_BTNSHADOW;
  p1[6] := COLOR_BTNTEXT;
  p1[7] := COLOR_GRAYTEXT;
  p1[8] := COLOR_INACTIVEBORDER;
  p1[9] := COLOR_INACTIVECAPTION;
  {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
  p1[10] := COLOR_MENU;
  p1[11] := COLOR_SCROLLBAR;
  p2[1] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
  p2[2] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
  p2[3] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
  p2[4] := 192 * 16 * 16 * 16 * 16 + 184 * 16 * 16 + 203;
  p2[5] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
  p2[6] := 0;
  p2[7] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
  p2[8] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
  p2[9] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
  p2[10] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
  p2[11] := 192 * 16 * 16 * 16 * 16 + 184 * 16 * 16 + 203;
  setsyscolors(11, p1, p2);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  p1: tarr;
begin
  p1[1] := COLOR_ACTIVEBORDER;
  p1[2] := COLOR_APPWORKSPACE;
  p1[3] := COLOR_BTNFACE;
  p1[4] := COLOR_BTNHILIGHT;
  p1[5] := COLOR_BTNSHADOW;
  p1[6] := COLOR_BTNTEXT;
  p1[7] := COLOR_GRAYTEXT;
  p1[8] := COLOR_INACTIVEBORDER;
  p1[9] := COLOR_INACTIVECAPTION;
  {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
  p1[10] := COLOR_MENU;
  p1[11] := COLOR_SCROLLBAR;
  setsyscolors(11, p1, ind);
end;

procedure TForm1.FormDeactivate(Sender: TObject);
var
  p1: tarr;
begin
  p1[1] := COLOR_ACTIVEBORDER;
  p1[2] := COLOR_APPWORKSPACE;
  p1[3] := COLOR_BTNFACE;
  p1[4] := COLOR_BTNHILIGHT;
  p1[5] := COLOR_BTNSHADOW;
  p1[6] := COLOR_BTNTEXT;
  p1[7] := COLOR_GRAYTEXT;
  p1[8] := COLOR_INACTIVEBORDER;
  p1[9] := COLOR_INACTIVECAPTION;
  {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
  p1[10] := COLOR_MENU;
  p1[11] := COLOR_SCROLLBAR;
  setsyscolors(11, p1, ind);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ind[1] := getsyscolor(COLOR_ACTIVEBORDER);
  ind[2] := getsyscolor(COLOR_APPWORKSPACE);
  ind[3] := getsyscolor(COLOR_BTNFACE);
  ind[4] := getsyscolor(COLOR_BTNHILIGHT);
  ind[5] := getsyscolor(COLOR_BTNSHADOW);
  ind[6] := getsyscolor(COLOR_BTNTEXT);
  ind[7] := getsyscolor(COLOR_GRAYTEXT);
  ind[8] := getsyscolor(COLOR_INACTIVEBORDER);
  ind[9] := getsyscolor(COLOR_INACTIVECAPTION);
  {ind[9] := getsyscolor(COLOR_INACTIVECAPTIONTEXT);}
  ind[10] := getsyscolor(COLOR_MENU);
  ind[11] := getsyscolor(COLOR_SCROLLBAR);
  application.OnDeactivate := formdeactivate;
  application.OnActivate := button2click;
end;

end.

<< Back to main page