4

フォームのコントロールの色を無効にする例に従おうとしています。

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

しかし、サード パーティ コントロール (infopower TwwDBComboDlg) または標準の VCL TEdit を登録または登録解除すると、例外が発生します。誰でもこれまたは提案に問題がありました

4

1 に答える 1

3

ここのリンクは、知っておくべきことを説明しています。

基本的に、既に知っている "null フック" を入れるか、欠落しているものの半分である "VCL の色" フックを入れる必要があります。残りの半分は、nil ポインターの問題です。

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 が nil であり、呼び出している関数を呼び出さずに、そのコードを確認してから戻る必要があることです。見逃した場合に備えて、テーマを有効にする場所は次のとおりです。

ここに画像の説明を入力

興味深い補足: VCL Styles utilsライブラリを入手してください。これを使用して物の色を変更する例を次に示します。

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

スタイルを作成し、それらのスタイルを特定のコントロールに適用し、テーマ エンジンを拡張することもできます。VCL Styles Utils ツールを使用して目的の結果を得ることができるかもしれませんが、簡単ではありません。

于 2012-02-03T19:10:27.757 に答える