| 
			Author: Peter Below
How can I change the color of a disabled (Edit1.Enabled := false;) control? I do 
not want the normal grey color.
Answer:
Two options: Place the control on a panel and disable the panel instead of the 
control. This way the color stays to whatever you set it. Or make a descendent and 
take over the painting when it is disabled. Here is an example:
1   unit PBExEdit;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
7   StdCtrls;
8   
9   type
10    TPBExEdit = class(Tedit)
11    private
12      { Private declarations }
13      FDisabledColor: TColor;
14      FDisabledTextColor: TColor;
15      procedure WMPaint(var msg: TWMPaint); message WM_PAINT;
16      procedure WMEraseBkGnd(var msg: TWMEraseBkGnd); message WM_ERASEBKGND;
17      procedure SetDisabledColor(const Value: TColor); virtual;
18      procedure SetDisabledTextColor(const Value: TColor); virtual;
19    protected
20      { Protected declarations }
21    public
22      { Public declarations }
23      constructor Create(aOwner: TComponent); override;
24    published
25      { Published declarations }
26      property DisabledTextColor: TColor read FDisabledTextColor write
27        SetDisabledTextColor
28        default clGrayText;
29      property DisabledColor: TColor read FDisabledColor write SetDisabledColor 
30  default
31        clWindow;
32    end;
33  
34  procedure register;
35  
36  implementation
37  
38  procedure register;
39  begin
40    RegisterComponents('PBGoodies', [TPBExEdit]);
41  end;
42  
43  constructor TPBExEdit.Create(aOwner: TComponent);
44  begin
45    inherited;
46    FDisabledColor := clWindow;
47    FDisabledTextColor := clGrayText;
48  end;
49  
50  procedure TPBExEdit.SetDisabledColor(const Value: TColor);
51  begin
52    if FDisabledColor <> Value then
53    begin
54      FDisabledColor := Value;
55      if not Enabled then
56        Invalidate;
57    end;
58  end;
59  
60  procedure TPBExEdit.SetDisabledTextColor(const Value: TColor);
61  begin
62    if FDisabledTextColor <> Value then
63    begin
64      FDisabledTextColor := Value;
65      if not Enabled then
66        Invalidate;
67    end;
68  end;
69  
70  procedure TPBExEdit.WMEraseBkGnd(var msg: TWMEraseBkGnd);
71  var
72    canvas: TCanvas;
73  begin
74    if Enabled then
75      inherited
76    else
77    begin
78      canvas := TCanvas.Create;
79      try
80        canvas.Handle := msg.DC;
81        SaveDC(msg.DC);
82        try
83          canvas.Brush.Color := FDisabledColor;
84          canvas.Brush.Style := bsSolid;
85          canvas.Fillrect(clientrect);
86          msg.result := 1;
87        finally
88          RestoreDC(msg.DC, -1);
89        end;
90      finally
91        canvas.free
92      end;
93    end;
94  end;
95  
96  procedure TPBExEdit.WMPaint(var msg: TWMPaint);
97  var
98    canvas: TCanvas;
99    ps: TPaintStruct;
100   callEndPaint: Boolean;
101 begin
102   if Enabled then
103     inherited
104   else
105   begin
106     callEndPaint := False;
107     canvas := TCanvas.Create;
108     try
109       if msg.DC <> 0 then
110       begin
111         canvas.Handle := msg.DC;
112         ps.fErase := true;
113       end
114       else
115       begin
116         BeginPaint(handle, ps);
117         callEndPaint := true;
118         canvas.handle := ps.hdc;
119       end;
120       if ps.fErase then
121         Perform(WM_ERASEBKGND, canvas.handle, 0);
122       SaveDC(canvas.handle);
123       try
124         canvas.Brush.Style := bsClear;
125         canvas.Font := Font;
126         canvas.Font.Color := FDisabledTextColor;
127         canvas.TextOut(1, 1, Text);
128       finally
129         RestoreDC(canvas.handle, -1);
130       end;
131     finally
132       if callEndPaint then
133         EndPaint(handle, ps);
134       canvas.free
135     end;
136   end;
137 end;
138 
139 end.
			 |