简体   繁体   English

Delphi XE2:在组件上禁用vcl Style

[英]Delphi XE2: Disabling vcl Style on a component

I am trying to follow the example about to disable the color on a control on my form. 我试图按照示例禁用表单上控件的颜色。

TStyleManager.Engine.RegisterStyleHook(ClrMeans.TwwDBComboDLG, TEditStyleHook); TStyleManager.Engine.RegisterStyleHook(ClrMeans.TwwDBComboDLG,TEditStyleHook);

But I am getting an exception when either registering or unregistering either a the 3rd party control (infopower TwwDBComboDlg) or a standard VCL TEdit. 但是,当注册或取消注册第三方控件(infopower TwwDBComboDlg)或标准VCL TEdit时,我会收到异常。 Anybody had any issues with this or any suggestions 任何人对此或任何建议都有任何疑问

The link here explains what you need to know. 此处的链接说明了您需要了解的内容。

Basically, you need to either put a "null hook" in, which is what you already knew, or you need to put a "VCL colors" hook in, which is half of what you are missing. 基本上,你需要在其中放入一个“空钩子”,这是你已经知道的,或者你需要放入一个“VCL颜色”钩子,这是你所缺少的一半。 The other half is your nil pointer problem. 另一半是你的零指针问题。

To make TEdit derivatives (like yours) look like VCL standard colors the code you need to make it work with your control is this: 为了使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);

Your problem with NIL is that if you don't have VCL themes turned on, then Engine is nil, and you should check and just return from that code without calling that function you're calling. NIL的问题是,如果你没有打开VCL主题,那么Engine就是nil,你应该检查并从该代码返回而不调用你正在调用的那个函数。 Here's where you turn on the themes, in case you missed it: 在这里您可以打开主题,以防错过它:

在此输入图像描述

Interesting side stuff: Get the VCL Styles utils library. 有趣的一面:获取VCL Styles utils库。 Here's an example of using it to change colors of stuff: 这是一个使用它来改变东西颜色的例子:

 TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleColor(scEdit, clWindow);
 TCustomStyleExt(TStyleManager.ActiveStyle).SetStyleFontColor(sfEditBoxTextNormal
                   ,clWindowText);

You can create styles, and apply those styles to particular controls, and even expand the theming engine, it might be possible to use the VCL Styles Utils tool to get your desired result, but it will not be trivial. 您可以创建样式,并将这些样式应用于特定控件,甚至可以扩展主题引擎,也可以使用VCL样式实用工具来获得所需的结果,但这不是一件容易的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM