码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-25 23:02:25      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:

声明如下:

  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函数中设置刷子的颜色,然后填充整个背景

标签:

原文地址:http://www.cnblogs.com/findumars/p/5218601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!