Tema: Timer
Ver Mensaje Individual
  #1  
Antiguo 08-05-2008
[coso] coso is offline
Miembro Premium
 
Registrado: may 2008
Ubicación: Girona
Posts: 1.678
Reputación: 0
coso Va por buen camino
Timer

Aqui una form que va calculando el tiempo q necesita un proceso hasta acabar (como un progress bar pero en segundos y minutos) y un ejemplo de uso grafico (que aparte queda muy chulo XD).

Código Delphi [-]
unit timer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Buttons, StdCtrls;

type
  T_timer = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    SpeedButton1: TSpeedButton;
    Timer1: TTimer;
    procedure Inicia(te : longint; s : string);
    procedure Finaliza;
    procedure Siguiente;
    procedure Timer1Timer(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject); // boton abortar
    procedure FormActivate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    sec : longint;
    total_elem : longint;
    curr_elem  : longint;
  public
    abortado    : boolean;
    { Public declarations }
  end;

var
  _timer: T_timer;

implementation

{$R *.DFM}

procedure T_timer.Siguiente;
begin
        inc(curr_elem);
end;

procedure T_timer.Inicia(te : longint;s : string);
begin
        sec := 0;
        curr_elem  := 1;

        total_elem := te;
        abortado := false;

        caption := s;

        Timer1.Enabled := true;

        Show;
end;

procedure T_timer.Finaliza;
begin
        Timer1.Enabled := false;
        MessageDLG(Caption + ' finalizado.',mtInformation,[mbOk],0);
        visible := false;
        close;
end;

//Enlazar con el evento ontimer del timer1 en el form
procedure T_timer.Timer1Timer(Sender: TObject);
var
         hr,mn,sc : integer;
         he,me,se : integer;
         el       : longint;
         et       : longint;
begin
        Application.ProcessMessages;

        inc(sec);

        sc := sec mod 60;
        mn := (sec div 60) mod 60;
        hr := sec div 3600;

        Label3.Caption := FormatFloat('0',hr) + ':' + FormatFloat('00',mn) + ':' + FormatFloat('00',sc);

        el := curr_elem;
        et := sec*total_elem div el;

        se := et mod 60;
        me := (et div 60) mod 60;
        he := et div 3600;

        Label4.Caption := inttostr(he) + ':'+formatfloat('00',me) + ':' + formatfloat('00',se);
end;

// evento onclick del speedbutton
procedure T_timer.SpeedButton1Click(Sender: TObject);
begin
        abortado := True;
end;

//evento onactivate
procedure T_timer.FormActivate(Sender: TObject);
begin
        Label3.Caption := '---';
        Label4.Caption := '---';
end;

// evento onshow
procedure T_timer.FormShow(Sender: TObject);
begin
        if timer1.enabled = false then close;

end;

procedure T_timer.FormCreate(Sender: TObject);
begin
        Timer1.Enabled := false;
end;

end.

y un pequeño ejemplo

Código Delphi [-]

unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Ejemplo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


uses timer;

{$R *.DFM}

procedure TForm1.Ejemplo;
var
   tim : T_timer;
   i,r : longint;
   x,y : longint;
begin

    r := 10000000;
    tim := T_Timer.Create(self);
    tim.Inicia(r,'Pintando la main form...');

    for i := 0 to r do
    begin
       Application.ProcessMessages;
       x := random(width);
       y := random(Height);
       Form1.Canvas.Pixels[x,y] := ((x*y)*$FFFFFFF mod $FFFFFF) + random(1000);
       tim.Siguiente;
       if tim.Abortado then break;
    end;
    tim.Finaliza;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
        Ejemplo;
end;

end.
Responder Con Cita