Author: Jonas Bilinkevicius
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.
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls;
8
9 type
10 tarr = array[1..11] of integer;
11 TForm1 = class(TForm)
12 Button2: TButton;
13 procedure Button2Click(Sender: TObject);
14 procedure FormClose(Sender: TObject; var Action: TCloseAction);
15 procedure FormCreate(Sender: TObject);
16 private
17 { Private declarations }
18 procedure FormDeactivate(Sender: TObject);
19 public
20 { Public declarations }
21 end;
22
23 var
24 Form1: TForm1;
25 ind: tarr;
26
27 implementation
28
29 {$R *.DFM}
30
31 procedure TForm1.Button2Click(Sender: TObject);
32 var
33 p1, p2: tarr;
34 begin
35 p1[1] := COLOR_ACTIVEBORDER;
36 p1[2] := COLOR_APPWORKSPACE;
37 p1[3] := COLOR_BTNFACE;
38 p1[4] := COLOR_BTNHILIGHT;
39 p1[5] := COLOR_BTNSHADOW;
40 p1[6] := COLOR_BTNTEXT;
41 p1[7] := COLOR_GRAYTEXT;
42 p1[8] := COLOR_INACTIVEBORDER;
43 p1[9] := COLOR_INACTIVECAPTION;
44 {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
45 p1[10] := COLOR_MENU;
46 p1[11] := COLOR_SCROLLBAR;
47 p2[1] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
48 p2[2] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
49 p2[3] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
50 p2[4] := 192 * 16 * 16 * 16 * 16 + 184 * 16 * 16 + 203;
51 p2[5] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
52 p2[6] := 0;
53 p2[7] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
54 p2[8] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
55 p2[9] := 84 * 16 * 16 * 16 * 16 + 72 * 16 * 16 + 100;
56 p2[10] := 128 * 16 * 16 * 16 * 16 + 111 * 16 * 16 + 150;
57 p2[11] := 192 * 16 * 16 * 16 * 16 + 184 * 16 * 16 + 203;
58 setsyscolors(11, p1, p2);
59 end;
60
61 procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
62 var
63 p1: tarr;
64 begin
65 p1[1] := COLOR_ACTIVEBORDER;
66 p1[2] := COLOR_APPWORKSPACE;
67 p1[3] := COLOR_BTNFACE;
68 p1[4] := COLOR_BTNHILIGHT;
69 p1[5] := COLOR_BTNSHADOW;
70 p1[6] := COLOR_BTNTEXT;
71 p1[7] := COLOR_GRAYTEXT;
72 p1[8] := COLOR_INACTIVEBORDER;
73 p1[9] := COLOR_INACTIVECAPTION;
74 {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
75 p1[10] := COLOR_MENU;
76 p1[11] := COLOR_SCROLLBAR;
77 setsyscolors(11, p1, ind);
78 end;
79
80 procedure TForm1.FormDeactivate(Sender: TObject);
81 var
82 p1: tarr;
83 begin
84 p1[1] := COLOR_ACTIVEBORDER;
85 p1[2] := COLOR_APPWORKSPACE;
86 p1[3] := COLOR_BTNFACE;
87 p1[4] := COLOR_BTNHILIGHT;
88 p1[5] := COLOR_BTNSHADOW;
89 p1[6] := COLOR_BTNTEXT;
90 p1[7] := COLOR_GRAYTEXT;
91 p1[8] := COLOR_INACTIVEBORDER;
92 p1[9] := COLOR_INACTIVECAPTION;
93 {p1[9] := COLOR_INACTIVECAPTIONTEXT;}
94 p1[10] := COLOR_MENU;
95 p1[11] := COLOR_SCROLLBAR;
96 setsyscolors(11, p1, ind);
97 end;
98
99 procedure TForm1.FormCreate(Sender: TObject);
100 begin
101 ind[1] := getsyscolor(COLOR_ACTIVEBORDER);
102 ind[2] := getsyscolor(COLOR_APPWORKSPACE);
103 ind[3] := getsyscolor(COLOR_BTNFACE);
104 ind[4] := getsyscolor(COLOR_BTNHILIGHT);
105 ind[5] := getsyscolor(COLOR_BTNSHADOW);
106 ind[6] := getsyscolor(COLOR_BTNTEXT);
107 ind[7] := getsyscolor(COLOR_GRAYTEXT);
108 ind[8] := getsyscolor(COLOR_INACTIVEBORDER);
109 ind[9] := getsyscolor(COLOR_INACTIVECAPTION);
110 {ind[9] := getsyscolor(COLOR_INACTIVECAPTIONTEXT);}
111 ind[10] := getsyscolor(COLOR_MENU);
112 ind[11] := getsyscolor(COLOR_SCROLLBAR);
113 application.OnDeactivate := formdeactivate;
114 application.OnActivate := button2click;
115 end;
116
117 end.
|