2012-04-14 50 views
1

我想創建一個用戶選擇的樣式保存到ini文件(JvFormStorage和JVIniFileStorage)的窗體。我的問題是,如果我把我的代碼在OnCreate它不工作,在OnShow中的作品,但我得到的錯誤:設置變量風格創建/顯示窗體在Delphi XE2

"Cannot change Visible in OnShow or OnHide"

即使這是在OnShow中或在程序的唯一代碼調用(綠1一個菜單項,但會轉化爲下拉框選擇)即:

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; 

的工作,但與正常不閃爍窗戶'風格',如果可能的話,寧願不閃爍。

我可能會完全錯誤地解決這個問題。 感謝保羅

回答

4

在你的情況下,OnCreate事件是加載vcl樣式的正確位置。

這是一個最小的樣品運行的應用程序(應用程序必須包括 「碳」 和 「金的」 樣式)

項目代碼

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 
+0

感謝所有工作Paul – 2012-04-15 09:43:36