ActiveDelphi - Índice do Fórum ActiveDelphi
.: O site do programador Delphi! :.
 
 FAQFAQ   PesquisarPesquisar   MembrosMembros   GruposGrupos   RegistrarRegistrar 
 PerfilPerfil   Entrar e ver Mensagens ParticularesEntrar e ver Mensagens Particulares   EntrarEntrar 

Dica Interessante, Chamando um form de logon antes MainForm.

 
Novo Tópico   Responder Mensagem    ActiveDelphi - Índice do Fórum -> Delphi
Exibir mensagem anterior :: Exibir próxima mensagem  
Autor Mensagem
GodZilla_XF
Aprendiz
Aprendiz


Registrado: Quinta-Feira, 12 de Junho de 2003
Mensagens: 185
Localização: Água Boa - MT

MensagemEnviada: Sex Set 05, 2008 12:15 pm    Assunto: Dica Interessante, Chamando um form de logon antes MainForm. Responder com Citação

The MainForm of a Delphi application is a form (window) that is the first one created in the main body of the application.

If you need to implement some kind of authorization for your Delphi application you might want to display a login / password dialog *before* the main form is created and displayed to the user.

In short, the idea would be to create, display, and destroy the "login" dialog before creating the main form.

The Delphi MainForm

When a new Delphi project is created, "Form1" automatically becomes the value of the MainForm property (of the global Application object). To assign a different form to the MainForm property, use the Forms page of the Project|Options dialog box at design time.
When the main form closes, the application terminates.

Login/Password Dialog

Let's start by creating the main form of the application. Create a new Delphi project containing one form. This form is, by design, the main form.
If you change the name of the form to "TMainForm" and save the unit as "main.pas", the project's source code looks like (the project was saved as "PasswordApp"):

Código:

program PasswordApp;

uses
  Forms,
  main in 'main.pas' {MainForm};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm) ;
  Application.Run;
end.


Now, add a second form to the project.
By design, the second form added, gets listed in the "Auto-Create Forms" list on the Project Options dialog.

Name the second form "TLoginForm" and *remove* ir from the "Auto-Create Forms" list. Save the unit as "login.pas".

Add a Label, an Edit and a Button on the form.

Add a class method to create, show and close the login/password dialog. The method "Execute" returns true if the user has entered the correct password in the password edit box.

Here's the full source code:

Código:

unit login;

interface

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

type
  TLoginForm = class(TForm)
    LogInButton: TButton;
    pwdLabel: TLabel;
    passwordEdit: TEdit;
    procedure LogInButtonClick(Sender: TObject) ;
    procedure FormCreate(Sender: TObject) ;
  public
    class function Execute : boolean;
  end;

implementation
{$R *.dfm}

class function TLoginForm.Execute: boolean;
begin
  with TLoginForm.Create(nil) do
  try
    Result := ShowModal = mrOk;
  finally
    Free;
  end;
end;

procedure TLoginForm.LogInButtonClick(Sender: TObject) ;
begin
  if passwordEdit.Text = 'delphi' then
    ModalResult := mrOK
  else
    ModalResult := mrAbort;
end;

end.


The Execute method dynamically creates an instance of the TLoginForm and displays it modaly using the ShowModal method.
ShowModal does not return until the form closes. When the form closes, it returns the value of the ModalResult property.

The "LogInButton" OnClick event handler assigns "mrOk" to the ModalResult property is the user has entered the correct password ('delphi' in the above example). If the user has provided a wrong password, ModalResult is set to "mrAbort" (can be anything *except* "mrNone").

Setting a value to the ModalResult property closes the form ... Execute returns true is ModalResult equals "mrOk" - if the user has entered the correct password.

Don't Create MainForm Before Login

You now only need to make sure the main form is not created if the user failed to provide the correct password.
Here's how the project's source code should look :

Código:

program PasswordApp;

uses
  Forms,
  main in 'main.pas' {MainForm},
  login in 'login.pas' {LoginForm};

{$R *.res}

begin
  if TLoginForm.Execute then
  begin
    Application.Initialize;
    Application.CreateForm(TMainForm, MainForm) ;
    Application.Run;
  end
  else
  begin
    Application.MessageBox('You are not authorized to use the application. The password is "delphi".', 'Password Protected Delphi application') ;
  end;
end.


Note the usage of the if then else block to determine if the main form should be created. If "Execute" returns false, MainForm is not created and the application terminates without starting, so to say.
That's all!

fonte:
http://delphi.about.com/od/windowsshellapi/a/password_login.htm
_________________
<b>GodZilla_XF</b>
<b><i>Algoritimizando...</b></i>
Voltar ao Topo
Ver o perfil de Usuários Enviar Mensagem Particular MSN Messenger
Mostrar os tópicos anteriores:   
Novo Tópico   Responder Mensagem    ActiveDelphi - Índice do Fórum -> Delphi Todos os horários são GMT - 3 Horas
Página 1 de 1

 
Ir para:  
Enviar Mensagens Novas: Proibido.
Responder Tópicos Proibido
Editar Mensagens: Proibido.
Excluir Mensagens: Proibido.
Votar em Enquetes: Proibido.


Powered by phpBB © 2001, 2005 phpBB Group
Traduzido por: Suporte phpBB