delphi - 在 Delphi XE2 中的创建/显示表单上设置变量样式

标签 delphi delphi-xe2 vcl-styles

我正在尝试创建一个表单,并将用户选择的样式保存到 ini 文件(JvFormStorage 和 JVIniFileStorage)。我遇到的问题是,如果我将代码放在 OnCreate 中,它不起作用,在 OnShow 中可以工作,但出现错误:

"Cannot change Visible in OnShow or OnHide"

即使这是 OnShow 或过程调用中的唯一代码(绿色 1 是 MenuItem,但会转换为组合框选项) 即:

Procedure TForm1.ChangeTheme;
begin
if Assigned(TStyleManager.ActiveStyle) then Begin
 If (Green1.Checked) and (TStyleManager.ActiveStyle.Name<>'Light Green') then
  TStyleManager.TrySetStyle('Light Green') else
 ... else
 TStyleManager.TrySetStyle(fdefaultStyleName);
end;

还尝试过:

    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Form1.ChangeTheme;
    Form1.Show;
    Application.Run;

确实有效,但会从普通窗口闪烁到“样式”,并且如果可能的话,希望不闪烁。

我很可能以完全错误的方式解决这个问题。 谢谢保罗

最佳答案

在您的情况下,OnCreate 事件是加载 vcl 样式的正确位置。

这是一个最小的示例工作应用程序,(该应用程序必须包含“carbon”和“auric”样式)

项目代码

program Project2;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

表单代码

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Green1: TCheckBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
 Vcl.Styles,//including this unit init the vcl styles services.
 Vcl.Themes;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
 fdefaultStyleName : string;
begin
 fdefaultStyleName:='Auric';
 if StyleServices.Enabled then
   If (Green1.Checked) and (not SameText(TStyleManager.ActiveStyle.Name,'Carbon')) then
    TStyleManager.TrySetStyle('Carbon')
   else
   TStyleManager.TrySetStyle(fdefaultStyleName);
end;

dfm

object Form1: TForm1
  Left = 520
  Top = 299
  Caption = 'Form1'
  ClientHeight = 294
  ClientWidth = 534
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 32
    Top = 256
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
  end
  object Green1: TCheckBox
    Left = 32
    Top = 56
    Width = 97
    Height = 17
    Caption = 'Green1'
    TabOrder = 1
  end
end

关于delphi - 在 Delphi XE2 中的创建/显示表单上设置变量样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10151994/

相关文章:

Delphi XE2 - 嵌套类函数无法编译

delphi - TDateTimePicker delphi 获取年月

sql - 如何在Delphi中设置包含 '@'的sql查询参数

delphi - 如何在 C++ Builder 中使用 Delphi "in"运算符

user-interface - 使用 TPanel 的哪个属性来获取此阴影?

delphi - 弹出菜单没有出现在我的 Delphi 中

delphi - REST Datasnap 覆盖 URI 映射

delphi - VirtualTreeView 和 VCL 样式

delphi - 启用运行时主题时,FTouchManager 会导致 AV

delphi - 我的应用程序的表单如何使用不同的 Vcl 样式?