我试图按照示例禁用表单上控件的颜色。
TStyleManager.Engine.RegisterStyleHook(ClrMeans.TwwDBComboDLG, TEditStyleHook);
但是,当我尝试注册或注销第三方控件(infopower TwwDBComboDlg)或标准VCL TEdit时,会出现异常。有人遇到过这个问题或有什么建议吗?
我试图按照示例禁用表单上控件的颜色。
TStyleManager.Engine.RegisterStyleHook(ClrMeans.TwwDBComboDLG, TEditStyleHook);
但是,当我尝试注册或注销第三方控件(infopower TwwDBComboDlg)或标准VCL TEdit时,会出现异常。有人遇到过这个问题或有什么建议吗?
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);
有趣的副产品:获取VCL Styles utils库。以下是使用它更改内容颜色的示例:
TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleColor(scEdit, clWindow);
TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleFontColor(sfEditBoxTextNormal
,clWindowText);