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.


  • 相关阅读:
    PIL库,图像处理第三方库
    文件指针
    机器学习之KNN---k最近邻算法-机器学习
    python 中的内置高级函数
    sklearn中standardscaler中fit_transform()和transform()有什么区别,应该怎么使用?
    python中导入sklearn中模块提示ImportError: DLL load failed: 找不到指定的程序。
    pandas中读取文件报错
    beacon帧字段结构最全总结(一)——beacon基本结构
    python中实例方法,类方法,静态方法简单理解
    一种logging封装方法,不会产生重复log
  • 原文地址:https://www.cnblogs.com/jankerxp/p/7774024.html
Copyright © 2011-2022 走看看