运行

时间:2015-08-12 10:11:09

标签: delphi runtime components

我正在使用Delhi XE2来创建自定义进度条组件。我以为我有它工作,但如果我使用进度条运行应用程序,完整的布局会发生变化。 红色,黄色和黑色面板消失,只有(主要)绿色面板可见。

这是我的代码:

unit CustomProgressBar;

interface

uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls;

type
  TCustomProgresBar = class(TPanel)
  private
    { Private declarations }
    LeftRedPanel: TPanel;
    LeftYellowPanel: TPanel;
    RightRedPanel: TPanel;
    RightYellowPanel: TPanel;
    BlackPanel: TPanel;
    function GetLeftRedPanelWidth: Integer;
    procedure SetLeftRedPanelWidth(const NewWidth: Integer);
    function GetLeftYellowPanelWidth: Integer;
    procedure SetLeftYellowPanelWidth(const NewWidth: Integer);
    function GetRightRedPanelWidth: Integer;
    procedure SetRightRedPanelWidth(const NewWidth: Integer);
    function GetRightYellowPanelWidth: Integer;
    procedure SetRightYellowPanelWidth(const NewWidth: Integer);
    function GetPosition: Integer;
    procedure SetPosition(const NewPosition: Integer);
  protected
    { Protected declarations }
    procedure Paint; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    property LeftRedPanelWidth: integer read GetLeftRedPanelWidth write SetLeftRedPanelWidth;
    property LeftYellowPanelWidth: integer read GetLeftYellowPanelWidth write SetLeftYellowPanelWidth;
    property RightRedPanelWidth: integer read GetRightRedPanelWidth write SetRightRedPanelWidth;
property RightYellowPanelWidth: integer read GetRightYellowPanelWidth write SetRightYellowPanelWidth;
    property Position: integer read GetPosition write SetPosition;
  end;



procedure Register;

implementation

procedure TCustomProgresBar.Paint;
begin
  inherited;
  BlackPanel.Height:=Height;
end;


function TCustomProgresBar.GetLeftRedPanelWidth: Integer;
begin
  Result := LeftRedPanel.Width;
end;

procedure TCustomProgresBar.SetLeftRedPanelWidth(const NewWidth: Integer);
begin
  if NewWidth <> LeftRedPanel.Width then
  begin
    LeftRedPanel.Width := NewWidth;
    Invalidate; // redraws the component
  end;
end;

function TCustomProgresBar.GetLeftYellowPanelWidth: Integer;
begin
  Result := LeftYellowPanel.Width;
end;

procedure TCustomProgresBar.SetLeftYellowPanelWidth(const NewWidth: Integer);
begin
  if NewWidth <> LeftYellowPanel.Width then
  begin
    LeftYellowPanel.Width := NewWidth;
    Invalidate; // redraws the component
  end;
end;

function TCustomProgresBar.GetRightRedPanelWidth: Integer;
begin
  Result := RightRedPanel.Width;
end;

procedure TCustomProgresBar.SetRightRedPanelWidth(const NewWidth: Integer);
begin
  if NewWidth <> RightRedPanel.Width then
  begin
    RightRedPanel.Width := NewWidth;
    Invalidate; // redraws the component
  end;
end;

function TCustomProgresBar.GetRightYellowPanelWidth: Integer;
begin
  Result := RightYellowPanel.Width;
end;

procedure TCustomProgresBar.SetRightYellowPanelWidth(const NewWidth: Integer);
begin
  if NewWidth <> RightYellowPanel.Width then
  begin
    RightYellowPanel.Width := NewWidth;
    Invalidate; // redraws the component
  end;
end;

function TCustomProgresBar.GetPosition: Integer;
begin
  Result := BlackPanel.Left;
end;

procedure TCustomProgresBar.SetPosition(const NewPosition: Integer);
begin
  if NewPosition <> BlackPanel.Left then
  begin
    BlackPanel.Left := NewPosition;
    Invalidate; // redraws the component
  end;
end;


procedure Register;
begin
  RegisterComponents('MarcelS', [TCustomProgresBar]);

end;

constructor TCustomProgresBar.Create(AOwner: TComponent);
begin
  inherited create(Aowner);
  Color:=65280;  //Green
  Width:=200;
  Height:=25;
  BevelOuter:=bvNone;

  LeftRedPanel:=TPanel.Create(Self);
  with LeftRedPanel do
  begin
    Parent := Self;
    Align:=alLeft;
    Color:=255;    //Red
    Width:=25;
    Caption:='';
    BevelOuter:=bvNone;
  end;

  LeftYellowPanel:=TPanel.Create(Self);
  with LeftYellowPanel do
  begin
    Parent := Self;
    Align:=alLeft;
    Color:=65535;   //Yellow
    Width:=25;
    Caption:='';
    BevelOuter:=bvNone;
  end;

  RightYellowPanel:=TPanel.Create(Self);
  with RightYellowPanel do
  begin
    Parent := Self;
    Align:=alRight;
    Color:=65535;   //Yellow
    Width:=25;
    Caption:='';
    BevelOuter:=bvNone;
  end;

  RightRedPanel:=TPanel.Create(Self);
  with RightRedPanel do
  begin
    Parent := Self;
    Align:=alRight;
    Color:=255;   //Red
    Width:=25;
    Caption:='';
    BevelOuter:=bvNone;
  end;

  BlackPanel:=TPanel.Create(Self);
  with BlackPanel do
  begin
    Parent := Self;
    Align:=alNone;
    Color:=0;   //Red
    Width:=5;
    Left:=35;
    Caption:='';

    Top:=0;
    BevelOuter:=bvNone;
  end;

  BlackPanel.Height:=Height;
  Caption:='';
end;

end.

0 个答案:

没有答案