DevMedia - asp.net, Java, Delphi, SQL e web Design, tudo em um só lugar!
Bem vindo a DevMedia!
LOGIN:     SENHA:
 
 

Fórum DevMedia


Autor
Mensagem
JOSE LUIZ DE MEDEIROS
 

País: Brasil
Estado: MG
Cidade: Ponte Nova
Mensagens: 76
 Postado em: 19/3/2010 10:30:20 PM

Colegas..
 
 
Como consigo manter uma memsagem tipo showmessage('Aguarde...') enquanto executo um loop longo?
 
 
 
 
Grato:
 
Jose Luiz
Emerson
 
 


País: Brasil
Estado: SP
Cidade: São Paulo
Mensagens: 938
 Postado em: 20/3/2010 3:05:49 PM
eu criei uma classe para montar a mensagem, com um form personalizado. assim:

unit Unit_Msg;

interface

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

type
  TForm_Msg = class(TForm)
    Label_Mensagem: TLabel;
    Panel_Rodape: TPanel;
    Panel_Progresso: TPanel;
    ProgressBar_Progresso: TProgressBar;
    Panel_Cancela: TPanel;
    BitBtn_Cancela: TBitBtn;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure BitBtn_CancelaClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Cancelou: boolean;
  end;

  TMsgLoop = class
  private
    FForm_Msg: TForm_Msg;
    FJanelas: Pointer;
  public
    constructor Create;
    procedure AddProgresso(Incremento: integer = 1);
    procedure Mensagem(Titulo, Mensagem: string; Progresso: integer = 0;
     BtnCancel: boolean = False);
    function FechaMensagem: boolean;
    destructor Destroy; override;
    function Cancelou: boolean;
  end;

implementation

{$R *.dfm}

{ TMsgLoop }

procedure TMsgLoop.AddProgresso(Incremento: integer);
begin
  FForm_Msg.ProgressBar_Progresso.Position := FForm_Msg.ProgressBar_Progresso.Position + Incremento;
end;

constructor TMsgLoop.Create;
begin
  inherited;

  FForm_Msg := TForm_Msg.Create(nil);
end;

destructor TMsgLoop.Destroy;
begin
  FreeAndNil(FForm_Msg);

  inherited;
end;

procedure TMsgLoop.Mensagem(Titulo, Mensagem: string; Progresso: integer = 0;
 BtnCancel: boolean = False);
begin
  FForm_Msg.Cancelou := False;
  FForm_Msg.Caption := Titulo;
  FForm_Msg.Label_Mensagem.Caption := ' '+Mensagem;
  FForm_Msg.BitBtn_Cancela.Visible := BtnCancel;
  FJanelas := DisableTaskWindows(FForm_Msg.Handle);
  if Progresso > 0 then
    FForm_Msg.ProgressBar_Progresso.Max := Progresso;
  FForm_Msg.ProgressBar_Progresso.Visible := (Progresso > 0);
  FForm_Msg.ProgressBar_Progresso.Position := 0;
  FForm_Msg.Panel_Cancela.Visible := BtnCancel;
  FForm_Msg.Show;
  FForm_Msg.Update;
end;


function TMsgLoop.FechaMensagem: boolean;
begin
  try
    EnableTaskWindows(FJanelas);
    FForm_Msg.Close;
    FreeAndNil(FForm_Msg);
    Result := True;
  except
    Result := False;
  end;
end;

function TMsgLoop.Cancelou: boolean;
begin
  Result := FForm_Msg.Cancelou;
end;

{TForm_Msg}

procedure TForm_Msg.BitBtn_CancelaClick(Sender: TObject);
begin
  Cancelou := True;
end;

procedure TForm_Msg.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
end;

end.

essa é a aparência de TForm_Msg:
Formulário








e, para exemplo, você usar este código:
unit Unit_Principal;

interface

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

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

var
  Form1: TForm1;

implementation

uses Unit_Msg;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Msg: TMsgLoop;
  i: integer;
begin
  Msg := TMsgLoop.Create;
  Msg.Mensagem('Processando loop de 1 até 1000', 'Contando. Aguarde...', 1000, True);
  i := 0;
  try
    while (i < 1000) and not Msg.Cancelou do
    begin
      Msg.AddProgresso;
      Inc(i);
      Sleep(10);
      Application.ProcessMessages;
    end;
  finally
    Msg.FechaMensagem;
  end;
  Msg.Free;
end;

end.






 
JOSE LUIZ DE MEDEIROS
 

País: Brasil
Estado: MG
Cidade: Ponte Nova
Mensagens: 76
 Postado em: 20/3/2010 4:53:37 PM
Valeu.

Deu certo, muito obrigado..

Jose luiz

 
web-03
DevMedia  |  Anuncie  |  Fale conosco
Hospedagem web por Porta 80 Web Hosting
2013 - Todos os Direitos Reservados a web-03