Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 08-05-2008
[coso] coso is offline
Miembro Premium
 
Registrado: may 2008
Ubicación: Girona
Posts: 1.678
Poder: 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
  #2  
Antiguo 14-07-2008
madmai madmai is offline
Miembro
 
Registrado: oct 2005
Posts: 117
Poder: 19
madmai Va por buen camino
he estado trabajando con este truco y he de decir que esta muy bien pero cabe reseñar que cuando se hace con procesos largos por ejemplo de mas de 3 minutos yo he tenido que declarar las variables que aqui estan declaradas como longint, las he tenido que declarar como int64.
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 01:16:28.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi