红鱼儿

How to works the "Styles" and how create it in coding on FMX projects? by Emailx45

盒子上的朋友Emailx45,发表这篇文章,用代码建立FMX的Styles并如何使用。原文是英文发表的,我大致译过来:

场景:

-- RAD Studio 11.1 Alexandria
-- FireMonkey 32x/64x

-- 思考……


——思考……

如何使用“Styles”以及如何在 FMX 项目中通过代码来创建它?

首先,让我们了解一下,更不用说,这一切是如何运作的!

当然,我不是“创造者”,那样的话,我没有所有的细节......

应用程序(项目)的默认样式将驻留在“应用程序”对象中,并将保存在项目的资源文件中。

RAD Studio 对于FireMonkey 框架,在其内部定义中包含了已知的至少 3 种样式的所有基本定义,即:Win styles, Next Style and iOS Style.

所以“它”不会显示在您的表单或项目中的其他任何地方......(当然,如果您有一个“BookStyle”组件,您可以看到从您选择的样式文件加载的定义)。

现在是最有趣的部分:文件中保存的样式实际上是与表单资源文件(*.FMX)相同或非常相似的资源文件(*.Style)。

因此,这些文件中包含的资源在项目打开(显示在 IDE 中)和/或编译(在软件运行时使用)时被解释。

因此,您可以在 FireMonkey 中手动编辑“*.Style”文件。

另一方面,使用样式的 VCL 项目有另一种使用此类资源的方式。因此,这两个框架及其资源文件之间没有兼容性。

在这篇简短的论文之后,让我们开始通过代码编写我们自己的Styles。

这一切都从创建一个“container”开始,以容纳新Style使用的所有元素。

对于 FireMonkey,最好的类是“TLayout”,原因很明显:它不会出现在屏幕上(透明),它可以在项目中大量使用,它可以容纳其他容器(视觉与否)等...

“TLayout”广泛用于在 FireMonkey 中创建Style。

在StyleBook对象的样式编辑器中,您会发现“TLayout”对象容纳了样式定义中的其他对象。

现在,了解了这么多,是可以按照自己的意愿创建自己的Style的时候了。

注意:有些样式以“二进制”方式存储在文件中,但是通过适合此任务的函数以相同的方式读取。

祝你好运。

以下是具体的代码:

var
  ViewFormMain: TViewFormMain;

implementation

{$R *.fmx}

var
  MyStyleLayout          : TLayout;
  MyStyleLayoutSurface         : TRectangle;
  MyStyleLayoutSurfaceAnim     : TRectAnimation;
  MyStyleLayoutSurfaceColorAnim: TColorAnimation;
  MyStyleLayoutGlyph          : TImage;
  MyStyleLayoutGlyphFloatAnim  : TFloatAnimation;
  MyStyleLayoutText          : TText;
  MyStyleLayoutTextColorAnim   : TColorAnimation;
  MyStyleLayoutInvertColors    : TInvertEffect;
  MyNewStyleCreated          : boolean = false;

function MyCreateMyNewStyle: boolean;
const
  MyStyleLayoutSurfaceColor: TAlphaColor       = TAlphaColorRec.Blue;
  MyStyleLayoutSurfaceStrokeColor: TAlphaColor = TAlphaColorRec.Red;
  // MyStyleLayoutSurfaceColorAnim: TAlphaColor          = TAlphaColorRec.Yellow;
  MyStyleLayoutTextTextSettingsFontColor: TAlphaColor     = TAlphaColorRec.Yellow;
  MyStyleLayoutTextTextSettingsFontColorAnim: TAlphaColor = TAlphaColorRec.Blue;
begin
  result := false;
  //
  // if MyStyleLayout=nil then create..., if MyStyleLayoutSurface=nil then create..., etc......
  //
  // I'm not taking care about exception ok!!! >:)))
  //
  MyStyleLayout          := TLayout.Create(ViewFormMain);
  MyStyleLayout.Name      := 'MyStyleLayout';
  MyStyleLayout.StyleName := 'mystylelayoutname';
  // MyStyleLayout.Parent    := ViewFormMain;
  //
  MyStyleLayoutSurface          := TRectangle.Create(ViewFormMain);
  MyStyleLayoutSurface.Name          := 'MyStyleLayoutSurface';
  MyStyleLayoutSurface.StyleName        := 'background'; // dont translate
  MyStyleLayoutSurface.Align          := TAlignLayout.Client;
  MyStyleLayoutSurface.CornerType       := TCornerType.InnerLine;
  MyStyleLayoutSurface.Fill.Color       := MyStyleLayoutSurfaceColor;
  MyStyleLayoutSurface.HitTest          := false;
  MyStyleLayoutSurface.Stroke.Color     := MyStyleLayoutSurfaceStrokeColor;
  MyStyleLayoutSurface.Stroke.Thickness := 5.0;
  MyStyleLayoutSurface.XRadius          := 50.0;
  MyStyleLayoutSurface.YRadius          := 50.0;
  MyStyleLayoutSurface.Parent          := MyStyleLayout;
  //
  MyStyleLayoutSurfaceAnim          := TRectAnimation.Create(ViewFormMain);
  MyStyleLayoutSurfaceAnim.Name          := 'MyStyleLayoutSurfaceAnim';
  MyStyleLayoutSurfaceAnim.Duration         := 0.2;
  MyStyleLayoutSurfaceAnim.PropertyName     := 'Margins';
  MyStyleLayoutSurfaceAnim.StopValue.Left   := 10.0;
  MyStyleLayoutSurfaceAnim.StopValue.Top    := 10.0;
  MyStyleLayoutSurfaceAnim.StopValue.Right  := 10.0;
  MyStyleLayoutSurfaceAnim.StopValue.Bottom := 10.0;
  MyStyleLayoutSurfaceAnim.Trigger          := 'IsMouseOver=true';
  MyStyleLayoutSurfaceAnim.TriggerInverse   := 'IsMouseOver=false';
  MyStyleLayoutSurfaceAnim.Parent          := MyStyleLayoutSurface;
  //
  MyStyleLayoutInvertColors         := TInvertEffect.Create(ViewFormMain);
  MyStyleLayoutInvertColors.Name    := 'MyStyleLayoutInvertColors';
  MyStyleLayoutInvertColors.Enabled := false;
  MyStyleLayoutInvertColors.Trigger := 'IsMouseOver=true';
  MyStyleLayoutInvertColors.Parent  := MyStyleLayoutSurface;
  //
  (* MyStyleLayoutSurfaceColorAnim          := TColorAnimation.Create(ViewFormMain);
    MyStyleLayoutSurfaceColorAnim.Name          := 'MyStyleLayoutSurfaceColorAnim';
    MyStyleLayoutSurfaceColorAnim.Duration       := 0.2;
    MyStyleLayoutSurfaceColorAnim.PropertyName   := 'Fill.Color';
    MyStyleLayoutSurfaceColorAnim.StartValue     := MyStyleLayoutSurfaceColor;
    MyStyleLayoutSurfaceColorAnim.StopValue      := MyStyleLayoutSurfaceColorAnim;
    MyStyleLayoutSurfaceColorAnim.Trigger        := 'IsMouseOver=true';
    MyStyleLayoutSurfaceColorAnim.TriggerInverse := 'IsMouseOver=false';
    MyStyleLayoutSurfaceColorAnim.Parent         := MyStyleLayoutSurface; *)
  //
  MyStyleLayoutText          := TText.Create(ViewFormMain);
  MyStyleLayoutText.Name          := 'MyStyleLayoutText';
  MyStyleLayoutText.StyleName          := 'text'; // dont translate
  MyStyleLayoutText.Align          := TAlignLayout.Center;
  MyStyleLayoutText.AutoSize          := true;
  MyStyleLayoutText.HitTest          := false;
  MyStyleLayoutText.Text          := 'Text';
  MyStyleLayoutText.TextSettings.FontColor := MyStyleLayoutTextTextSettingsFontColor;
  MyStyleLayoutText.Parent          := MyStyleLayoutSurface;
  //
  (* MyStyleLayoutTextColorAnim          := TColorAnimation.Create(ViewFormMain);
    MyStyleLayoutTextColorAnim.Name          := 'MyStyleLayoutTextColorAnim';
    MyStyleLayoutTextColorAnim.Duration       := 0.2;
    MyStyleLayoutTextColorAnim.PropertyName   := 'TextSettings.FontColor';
    MyStyleLayoutTextColorAnim.StartValue     := MyStyleLayoutTextTextSettingsFontColor;
    MyStyleLayoutTextColorAnim.StopValue      := MyStyleLayoutTextTextSettingsFontColorAnim;
    MyStyleLayoutTextColorAnim.Trigger        := 'IsMouseOver=true';
    MyStyleLayoutTextColorAnim.TriggerInverse := 'IsMouseOver=false';
    MyStyleLayoutTextColorAnim.Parent         := MyStyleLayoutText; *)
  //
  MyStyleLayoutGlyph          := TImage.Create(ViewFormMain);
  MyStyleLayoutGlyph.Name          := 'MyStyleLayoutGlyph';
  MyStyleLayoutGlyph.HitTest          := false;
  MyStyleLayoutGlyph.StyleName          := 'glyphstyle'; // dont translate
  MyStyleLayoutGlyph.MultiResBitmap.Height := 32;
  MyStyleLayoutGlyph.MultiResBitmap.Width  := 32;
  MyStyleLayoutGlyph.RotationAngle         := -45.0;
  MyStyleLayoutGlyph.MultiResBitmap.Add.Bitmap.LoadFromFile('D:\RADRIOTests\_Images_For_TImageList_and_Others\arrow_right-up.png');
  MyStyleLayoutGlyph.Parent := MyStyleLayout;
  //
  MyStyleLayoutGlyphFloatAnim          := TFloatAnimation.Create(ViewFormMain);
  MyStyleLayoutGlyphFloatAnim.Name          := 'MyStyleLayoutGlyphFloatAnim';
  MyStyleLayoutGlyphFloatAnim.Duration       := 0.2;
  MyStyleLayoutGlyphFloatAnim.PropertyName   := 'RotationAngle';
  MyStyleLayoutGlyphFloatAnim.StartValue     := -45.0;
  MyStyleLayoutGlyphFloatAnim.StopValue      := 45.0;
  MyStyleLayoutGlyphFloatAnim.Trigger        := 'IsMouseOver=true';
  MyStyleLayoutGlyphFloatAnim.TriggerInverse := 'IsMouseOver=false';
  MyStyleLayoutGlyphFloatAnim.Parent         := MyStyleLayoutGlyph;
  //
  result := true;
end;

procedure MyDestroyMyStyleToButtons;
begin
  // FreeAndNil(MyStyleLayoutGlyphFloatAnim);
  // FreeAndNil(MyStyleLayoutText);
  // FreeAndNil(MyStyleLayoutGlyph);
  // FreeAndNil(MyStyleLayoutSurfaceAnim);
  // FreeAndNil(MyStyleLayoutSurface);
  //
  FreeAndNil(MyStyleLayout); // delete all "owned"...
end;

procedure TViewFormMain.Btn_Creating_My_Crazy_StyleClick(Sender: TObject);
begin
  RadioButton3.Text := 'MyStyleLayout not created or destroyed...';
  //
  if (Self.FindComponent('MyStyleLayout') = nil) then
  begin
    MyNewStyleCreated := MyCreateMyNewStyle;
    //
    if MyNewStyleCreated then
    begin
      RadioButton3.Text := 'MyStyleLayout already created';
      //
      RadioButton1.OnClick(Self);
      RadioButton1.IsChecked := true;
    end
    else
      RadioButton3.Text := 'MyStyleLayout was not created';
  end else begin
    FreeAndNil(MyStyleLayout); // delete all "owned"...
    //
    RadioButton2.OnClick(Self);
    RadioButton2.IsChecked := true;
  end;
end;

procedure TViewFormMain.Button2Click(Sender: TObject);
begin
  // if not(Self.FindStyleResource('styletomybuttons', false) = nil) then ... if "Parent = Self {form1}" but this way it will appears on form.
  //
  if MyNewStyleCreated { or not(Self.FindComponent('MyStyleLayout') = nil) } then
    ShowMessage('MyStyleLayout was created')
  else
    ShowMessage('MyStyleLayout was not created');
end;

procedure TViewFormMain.RadioButton1Click(Sender: TObject);
begin
  if MyNewStyleCreated then
  begin
    Button2.StyleLookup      := 'mystylelayoutname';
    SpeedButton1.StyleLookup := 'mystylelayoutname';
    RadioButton1.StyleLookup := 'mystylelayoutname';
    Memo1.StyleLookup        := 'mystylelayoutname';
  end else begin
    Button2.StyleLookup      := '';
    SpeedButton1.StyleLookup := '';
    RadioButton1.StyleLookup := '';
    Memo1.StyleLookup        := '';
  end;
end;

procedure TViewFormMain.RadioButton2Click(Sender: TObject);
begin
  Button2.StyleLookup      := '';
  SpeedButton1.StyleLookup := '';
  RadioButton1.StyleLookup := '';
  Memo1.StyleLookup        := '';
end;

initialization

ReportMemoryLeaksOnShutdown := true;

finalization

end.

代码执行的效果: 

 

原文地址:http://bbs.2ccc.com/topic.asp?topicid=628182

向Emailx45再一次感谢!感谢他无私的分享!

后记,作者又给出了修正代码:

FIXING MY CODE ABOVE

MyStyleLayoutText          := TText.Create(nil);
  MyStyleLayoutText.Name          := 'MyStyleLayoutText';
  MyStyleLayoutText.StyleName          := 'text'; // dont translate
  MyStyleLayoutText.Align          := TAlignLayout.Center;
  MyStyleLayoutText.AutoSize          := true;
  MyStyleLayoutText.HitTest          := false;
  MyStyleLayoutText.Text          := 'Text';
  MyStyleLayoutText.TextSettings.FontColor := MyStyleLayoutTextTextSettingsFontColor;
 
 MyStyleLayoutText.Parent          := MyStyleLayout; // Layout-root

 

posted on 2022-04-16 07:40  红鱼儿  阅读(197)  评论(0编辑  收藏  举报