The link http://theroadtodelphi.wordpress.com/2012/02/06/changing-the-color-of-edit-controls-with-vcl-styles-enabled/这里解释了您需要了解的内容。
基本上,您需要放入一个“空钩子”,这是您已经知道的,或者需要放入一个“VCL 颜色”钩子,这是您所缺少的一半。另一半是你的零指针问题。
为了使 TEdit 衍生品(像您的)看起来像 VCL 标准颜色,使其与您的控件一起工作所需的代码如下:
uses
Winapi.Messages,
Vcl.Controls,
Vcl.StdCtrls,
Vcl.Forms,
Vcl.Themes,
Vcl.Styles;
type
TEditStyleHookColor = class(TEditStyleHook)
private
procedure UpdateColors;
protected
procedure WndProc(var Message: TMessage); override;
constructor Create(AControl: TWinControl); override;
end;
implementation
type
TWinControlH= class(TWinControl);
constructor TEditStyleHookColor.Create(AControl: TWinControl);
begin
inherited;
//call the UpdateColors method to use the custom colors
UpdateColors;
end;
//Here you set the colors of the style hook
procedure TEditStyleHookColor.UpdateColors;
var
LStyle: TCustomStyleServices;
begin
if Control.Enabled then
begin
Brush.Color := TWinControlH(Control).Color; //use the Control color
FontColor := TWinControlH(Control).Font.Color;//use the Control font color
end
else
begin
//if the control is disabled use the colors of the style
LStyle := StyleServices;
Brush.Color := LStyle.GetStyleColor(scEditDisabled);
FontColor := LStyle.GetStyleFontColor(sfEditBoxTextDisabled);
end;
end;
//Handle the messages of the control
procedure TEditStyleHookColor.WndProc(var Message: TMessage);
begin
case Message.Msg of
CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
begin
//Get the colors
UpdateColors;
SetTextColor(Message.WParam, ColorToRGB(FontColor));
SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end;
CM_ENABLEDCHANGED:
begin
//Get the colors
UpdateColors;
Handled := False;
end
else
inherited WndProc(Message);
end;
end;
Procedure ApplyVCLColorsStyleHook(ControlClass :TClass);
begin
if Assigned(TStyleManager.Engine) then
TStyleManager.Engine.RegisterStyleHook(ControlClass, TEditStyleHookColor);
end;
initialization
ApplyVCLColorsStyleHook(TwwDBComboDlg);
NIL 的问题是,如果您没有打开 VCL 主题,则 Engine 为零,您应该检查并从该代码返回,而不调用您正在调用的函数。如果您错过了,您可以在此处打开主题:
有趣的副业:获取VCL 样式实用程序 http://code.google.com/p/vcl-styles-utils/source/checkout图书馆。这是使用它来更改内容颜色的示例:
TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleColor(scEdit, clWindow);
TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleFontColor(sfEditBoxTextNormal
,clWindowText);
您可以创建样式,并将这些样式应用到特定控件,甚至扩展主题引擎,也许可以使用 VCL Styles Utils 工具来获得您想要的结果,但这并不简单。