zoukankan      html  css  js  c++  java
  • 任意改变FMX的TPanel控件的颜色

    FMX的控件的外观以Style为主,但是也有不方便的地方,比如要任意改变TPanel的颜色,只能写代码,摸索了几天,初步实现了

    unit FMX.JKPanelEx;
    
    
    interface
    
    
    uses
      System.SysUtils, System.Classes, System.Types, System.UITypes, FMX.Types, FMX.Controls, FMX.Objects, FMX.Graphics,
      FMX.Controls.Presentation, FMX.StdCtrls;
    
    
    type
      TAppearanceUpdateKind = (None, Fill, Stroke, CornerSize);
    
    
      TJKPanelEx = class(TPanel)
      private
        FAppearance: TRectangle;
        FFill: TBrush;
        FStroke: TStrokeBrush;
        FCornerSize: Single;
        FAppearanceUpdateKind: TAppearanceUpdateKind;
        procedure SetFill(const Value: TBrush);
        procedure SetStroke(const Value: TStrokeBrush);
        procedure SetCornerSize(const Value: Single);
    
    
      protected
        procedure ApplyStyle; override;
        procedure FreeStyle; override;
        procedure FillChanged(Sender: TObject); virtual;
        procedure StrokeChanged(Sender: TObject); virtual;
        procedure UpdateAppearanceFill;
        procedure UpdateAppearanceStroke;
        procedure UpdateAppearanceCornerSize;
        procedure UpdateAppearance;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    
    
        property Appearance: TRectangle read FAppearance write FAppearance;
      published
        property Fill: TBrush read FFill write SetFill;
        property Stroke: TStrokeBrush read FStroke write SetStroke;
        property CornerSize: Single read FCornerSize write SetCornerSize;
      end;
    
    
    procedure Register;
    
    
    implementation
    
    
    procedure Register;
    begin
      RegisterComponents('JKFMXControl', [TJKPanelEx]);
    end;
    
    
    { TJKPanelEx }
    
    
    procedure TJKPanelEx.ApplyStyle;
    begin
      inherited;
      if not Assigned(FAppearance) then
      begin
        if not FindStyleResource<TRectangle>('Background', FAppearance) and (ResourceControl is TRectangle) then
          FAppearance := TRectangle(ResourceControl);
        UpdateAppearanceCornerSize;
        UpdateAppearanceFill;
        UpdateAppearanceStroke;
      end
      else
      begin
        UpdateAppearance;
      end;
    end;
    
    
    constructor TJKPanelEx.Create(AOwner: TComponent);
    begin
      inherited;
      FAppearance := nil;
      FAppearanceUpdateKind := TAppearanceUpdateKind.None;
      FFill := TBrush.Create(TBrushKind.Solid, TAlphaColor($FFE0E0E0));
      FStroke := TStrokeBrush.Create(TBrushKind.Solid, TAlphaColors.Black);
      FFill.OnChanged := FillChanged;
      FStroke.OnChanged := StrokeChanged;
    end;
    
    
    destructor TJKPanelEx.Destroy;
    begin
      FreeAndNil(FFill);
      FreeAndNil(FStroke);
      inherited;
    end;
    
    
    procedure TJKPanelEx.FillChanged(Sender: TObject);
    begin
      FAppearanceUpdateKind := TAppearanceUpdateKind.Fill;
      ApplyStyle;
    end;
    
    
    procedure TJKPanelEx.FreeStyle;
    begin
      inherited;
      FAppearance := nil;
    end;
    
    
    
    
    procedure TJKPanelEx.SetCornerSize(const Value: Single);
    begin
      if FCornerSize <> Value then
      begin
        FAppearanceUpdateKind := TAppearanceUpdateKind.CornerSize;
        FCornerSize := Value;
        UpdateAppearanceCornerSize;
      end;
    end;
    
    
    procedure TJKPanelEx.SetFill(const Value: TBrush);
    begin
      FFill.Assign(Value);
    end;
    
    
    procedure TJKPanelEx.SetStroke(const Value: TStrokeBrush);
    begin
      FStroke.Assign(Value);
    end;
    
    
    procedure TJKPanelEx.StrokeChanged(Sender: TObject);
    begin
      FAppearanceUpdateKind := TAppearanceUpdateKind.Stroke;
      ApplyStyle;
    end;
    
    
    procedure TJKPanelEx.UpdateAppearance;
    begin
      case FAppearanceUpdateKind of
        TAppearanceUpdateKind.None: ;
        TAppearanceUpdateKind.Fill: UpdateAppearanceFill;
        TAppearanceUpdateKind.Stroke: UpdateAppearanceStroke;
        TAppearanceUpdateKind.CornerSize: UpdateAppearanceCornerSize;
      end;
    end;
    
    
    procedure TJKPanelEx.UpdateAppearanceFill;
    begin
      if FAppearance <> nil then
      begin
        FAppearance.Fill.Assign(FFill);
      end;
    end;
    
    
    procedure TJKPanelEx.UpdateAppearanceStroke;
    begin
      if FAppearance <> nil then
      begin
        FAppearance.Stroke.Assign(FStroke);
      end;
    end;
    
    
    procedure TJKPanelEx.UpdateAppearanceCornerSize;
    begin
      if FAppearance <> nil then
      begin
        FAppearance.XRadius := FCornerSize;
        FAppearance.YRadius := FCornerSize;
      end;
    end;
    
    
    end.


  • 相关阅读:
    【GruntMate】一个让你更方便使用Grunt的工具
    HTML5小游戏【是男人就下一百层】UI美化版
    【Grunt】关于Grunt可视化的尝试
    在腾讯ubuntu云服务器上面部署asp.net core 2.1网站
    存储过程中执行动态Sql语句
    我的2016年总结
    程序员的成长阶梯和级别定义
    让IE8在win7下面能显示使用window.showmodaldialog弹出窗口的地址状态栏
    更改计算机名称后 导致 sql server 2008 R2 用windows账户不能附加的错误解决办法
    【转】通过js获取系统版本以及浏览器版本
  • 原文地址:https://www.cnblogs.com/jankerxp/p/7774024.html
Copyright © 2011-2022 走看看