DBEdit принимает только цвет фона из Stylehook с выравниванием текста по левому краю - PullRequest
1 голос
/ 07 августа 2020

В настоящее время я нахожусь в процессе перехода на Delphi 10.3 и изучаю возможность включения стилей в свои приложения.

Во время этого процесса я пытаюсь реализовать собственный StyleHook, чтобы изменить цвет фона DBEdits в зависимости от определенных условий. Я сделал это аналогично руководству Rodri go Ruz см. Здесь

Во время тестирования я обнаружил проблему с отображением полей, для которых «Выравнивание» установлено на « taRightJustify ". Для этих DBEdits цвет фона просто игнорируется.

enter image description here

Upon further investigation i found, that it is only ignored as long as these are not focused.

введите описание изображения здесь

Это должно быть воспроизведено с помощью следующего кода

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, VCL.Styles, VCL.Themes,
  Vcl.Mask, Vcl.DBCtrls, Data.DB, Datasnap.DBClient;

type
  TForm2 = class(TForm)
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
TEditStyleHookColor = class(TEditStyleHook)
  private
    procedure UpdateColors;
  protected
    procedure WndProc(var Message: TMessage); override;
  public
    constructor Create(AControl: TWinControl); override;
  end;

type
 TWinControlH= class(TWinControl);

var
  Form2: TForm2;

implementation

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;

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  ClientDataSet1.FieldDefs.Add('LeftAlign',ftWideString,100);
  ClientDataSet1.FieldDefs.Add('RightAlign',ftWideString,100);
  ClientDataSet1.CreateDataSet;
  ClientDataSet1.FieldByName('RightAlign').Alignment := taRightJustify;

  ClientDataSet1.Insert;
  ClientDataSet1.Fields[0].AsString := 'testLeft';
  ClientDataSet1.Fields[1].AsString := 'testRight';

  DBEdit1.Color := clGreen;
  DBEdit2.Color := clGreen;
  DBEdit1.DataSource := DataSource1;
  DBEdit2.DataSource := DataSource1;
  DBEdit1.DataField := 'LeftAlign';
  DBEdit2.DataField := 'RightAlign';
end;

initialization
begin
  TStyleManager.Engine.RegisterStyleHook(TEdit, TEditStyleHookColor);
  TStyleManager.Engine.RegisterStyleHook(TDBEdit, TEditStyleHookColor);
end;

end.

Кто-нибудь знает, что здесь происходит?

...