How detect when a vcl style is changed?

2020-04-08 07:54发布

问题:

I use several WinAPi functions which needs the Handle of the form in order to work, due which the handle of the form is recreated when the vcl styles is changed many of the calls to these functions stop working. So I need a way to detect when the current vcl style is modified (changed) in order to update the calls to these functions.The question is How detect when a vcl style is changed?

回答1:

When a vcl style is changed via the TStyleManager.SetStyle method a CM_CUSTOMSTYLECHANGED message is sent to all the forms of the application, then that messgae is processed in the WndProc method of the form and then a CM_STYLECHANGED message is sent to inform which the vcl style has changed, so you can listen the CM_STYLECHANGED message to detect when a vcl style has changed.

Try this sample Code.

type
  TForm17 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure CMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
  public
    { Public declarations }
  end;

var
  Form17: TForm17;

implementation

uses
 Vcl.Themes;

{$R *.dfm}

procedure TForm17.Button1Click(Sender: TObject);
begin
   TStyleManager.SetStyle('Carbon');
end;

procedure TForm17.CMStyleChanged(var Message: TMessage);
begin
  ShowMessage('The vcl style has changed');
end;

end.