TPanel的默认颜色存储在dfm中,读取后在Paint函数中设置刷子的颜色,然后填充整个背景

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TPanel的默认颜色存储在dfm中,读取后在Paint函数中设置刷子的颜色,然后填充整个背景相关的知识,希望对你有一定的参考价值。

声明如下:

  TCustomPanel = class(TCustomControl)
  private
    FFullRepaint: Boolean;
    FParentBackgroundSet: Boolean;
    procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure Paint; override;
    property Color default clBtnFace; // 设置默认颜色,如果没有改变它的颜色,不用存储在dfm中
    property FullRepaint: Boolean read FFullRepaint write FFullRepaint default True;
    property ParentColor default False;
    procedure SetParentBackground(Value: Boolean); override;
  public
    property ParentBackground stored FParentBackgroundSet;
    constructor Create(AOwner: TComponent); override;
    function GetControlsAlignment: TAlignment; override;
  end;

当在窗体上放置一个TPanel的时候,就有:

  object Panel1: TPanel
    Left = 200
    Top = 232
    Width = 193
    Height = 153
    Caption = Panel1
    TabOrder = 2
  end

相当于此时的颜色默认就是clBtnFace。如果我在设计期改变它的color,那么会存储成:

  object Panel1: TPanel
    Left = 200
    Top = 232
    Width = 193
    Height = 153
    Caption = Panel1
    Color = clYellow
    TabOrder = 2
  end

最后观察一下它的Paint函数:

procedure TCustomPanel.Paint;
const
  Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
  Rect: TRect;
  TopColor, BottomColor: TColor;
  FontHeight: Integer;
  Flags: Longint;

  procedure AdjustColors(Bevel: TPanelBevel);
  begin
    TopColor := clBtnHighlight;
    if Bevel = bvLowered then TopColor := clBtnShadow;
    BottomColor := clBtnShadow;
    if Bevel = bvLowered then BottomColor := clBtnHighlight;
  end;

begin
  Rect := GetClientRect;
  if BevelOuter <> bvNone then
  begin
    AdjustColors(BevelOuter);
    Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
  end;
  Frame3D(Canvas, Rect, Color, Color, BorderWidth);
  if BevelInner <> bvNone then
  begin
    AdjustColors(BevelInner);
    Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
  end;
  with Canvas do
  begin
    if not ThemeServices.ThemesEnabled or not ParentBackground then
    begin
      Brush.Color := Color; // 就是这里设置刷子的颜色。如果把它改成clRed,当场就会让TPanel变成红色
      FillRect(Rect);
    end;
    Brush.Style := bsClear;
    Font := Self.Font;
    FontHeight := TextHeight(W);
    with Rect do
    begin
      Top := ((Bottom + Top) - FontHeight) div 2;
      Bottom := Top + FontHeight;
    end;
    Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[FAlignment];
    Flags := DrawTextBiDiModeFlags(Flags);
    DrawText(Handle, PChar(Caption), -1, Rect, Flags);
  end;
end;

 

以上是关于TPanel的默认颜色存储在dfm中,读取后在Paint函数中设置刷子的颜色,然后填充整个背景的主要内容,如果未能解决你的问题,请参考以下文章

从数据库读取图片路径后在页面上显示出来

将 TPanel 绘制到图像并将其保存在数据集中

DELPHI美化界面(2009开始TPanel增加了ParentBackGround)

五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时新增加子控件时重新创建当前控件的句柄时设置父控件时显示状态被改变时

Delphi 有时会将文本格式文件 (DFM) 恢复为二进制格式吗?

FastReport 程序员手册