Παρασκευή 11 Μαΐου 2012

Progressbar inside Button? No problem

It’s quite cool efect, but this is also effective way to save some space in your application GUI. Let’s place progressbar of some time-consuming function right into the button, which started this function.
 Place Button and Progressbar components on a form and set Visible property of Progressbar to False. Now, use the following code to do the magic.


unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  ProgressBar1.Parent := Button1;
  ProgressBar1.Width := Button1.Width - 7;
  ProgressBar1.Height := Button1.Height - 7;
  ProgressBar1.Left := 3;
  ProgressBar1.Top := 3;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var i : byte;
begin
  Button1.Enabled := false;
  if ProgressBar1.Visible then Exit;
  ProgressBar1.Position := 0;
  ProgressBar1.Visible := True;
 
  //replace with real code-----------
  for i := 0 to 99 do
  begin
    Sleep(100);
    ProgressBar1.Position := ProgressBar1.Position + 1;
    Application.ProcessMessages;
  end;
  //---------------------------------
 
  ProgressBar1.Visible := False;
  Button1.Enabled := true;
end;
 
end.
 
When you press the Button, the Progressbar will be set to visible 
(disabling the button itself). When the time-consuming function is over,
 the button will be reenabled again and progressbar will be set to 
invisible state. 

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου