sábado, 14 de abril de 2012

InputBox com Password (Janela de mensagem) no Delphi 7

Veja neste tutorial como criar um InputBox com Password, com um Edit e máscara para senha.


Este tutorial visa auxiliar quem precisa criar um janela de mensagem

que esconda o que está sendo digitado (senha). Primeiramente crie
as seguintes funções na sua Unit:



 function InputBoxPass(const ACaption, APrompt, ADefault: string): string;  
 function InputSenha(const ACaption, APrompt: string; var Value: string): Boolean;  
 function GetAveCharSize(Canvas: TCanvas): TPoint;  


E as implemente com os seguintes códigos:

 function TForm1.GetAveCharSize(Canvas: TCanvas): TPoint;   
  var   
  I: Integer;   
  Buffer: array[0..51] of Char;   
  begin   
  for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A'));   
  for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));   
  GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));   
  Result.X := Result.X div 52;   
  end;
Em seguida:

 function TForm1.InputBoxPass(const ACaption, APrompt,    
  ADefault: string): string;    
  begin    
  Result := ADefault;    
  InputSenha(ACaption, APrompt, Result);    
  end;   
E por fim:

 function TForm1.InputSenha(const ACaption, APrompt: string;    
  var Value: string): Boolean;    
  var    
  Form: TForm;    
  Prompt: TLabel;    
  Edit: TEdit;    
  DialogUnits: TPoint;    
  ButtonTop, ButtonWidth, ButtonHeight: Integer;    
  begin    
  Result := False;    
  Form := TForm.Create(Application);    
  with Form do    
  try    
   Canvas.Font := Font;    
   DialogUnits := GetAveCharSize(Canvas);    
   BorderStyle := bsDialog;    
   Caption := ACaption;    
   ClientWidth := MulDiv(180, DialogUnits.X, 4);    
   ClientHeight := MulDiv(63, DialogUnits.Y, 8);    
   Position := poMainformcenter;    
   Prompt := TLabel.Create(Form);    
   with Prompt do    
   begin    
   Parent := Form;    
   AutoSize := True;    
   Left := MulDiv(8, DialogUnits.X, 4);    
   Top := MulDiv(8, DialogUnits.Y, 8);    
   Caption := APrompt;    
   end;    
   Edit := TEdit.Create(Form);    
   with Edit do    
   begin    
   Parent := Form;    
   Left := Prompt.Left;    
   Top := MulDiv(19, DialogUnits.Y, 8);    
   Width := MulDiv(164, DialogUnits.X, 4);    
   {} MaxLength := 20;    
   {} Passwordchar := '*';    
   {} Font.Color := clBlue;    
   Text := Value;    
   SelectAll;    
   end;    
   ButtonTop := MulDiv(41, DialogUnits.Y, 8);    
   ButtonWidth := MulDiv(50, DialogUnits.X, 4);    
   ButtonHeight := MulDiv(14, DialogUnits.Y, 8);    
   with TButton.Create(Form) do    
   begin    
   Parent := Form;    
   Caption := 'Ok';    
   ModalResult := mrOk;    
   Default := True;    
   SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);    
   end;    
   with TButton.Create(Form) do    
   begin    
   Parent := Form;    
   Caption := 'Cancelar';    
   ModalResult := mrCancel;    
   Cancel := True;    
   SetBounds(MulDiv(92, DialogUnits.X, 4), ButtonTop, ButtonWidth,ButtonHeight);    
   end;    
   if ShowModal = mrOk then    
   begin    
   Value := Edit.Text;    
   Result := True;    
   end;    
  finally    
   Form.Free;    
  end;    
  end;   
Finalmente, adicione um Button e um Label no
formulário, e no evento onClick do botão,
digite:

 Label1.Caption := InputBoxPass('Login','Senha:','');  




Conclusões

Notamos neste artigo a facilidade em se criar um InputBox com
Password em detrimento de um ShowMessage comum
utilizando a ferramente Delphi 7. Forte abraço e até o próximo
tutorial.


--

Verificar se há conexão com a Internet no Delphi 7

Veja neste artigo como verificar se o computador no qual sua aplicação está rodando está conectado à Internet.


Primeiramente declare na Uses do seu Form a seguinte unit:


 uses  
  WinInet;  


Após isso, digite o que está abaixo na procedure
onClick do botão:


 procedure TForm1.Button1Click(Sender: TObject);  
 var  
  Flags : Cardinal;  
 begin  
  if not InternetGetConnectedState(@Flags, 0) then  
   ShowMessage('Não há conexão com a Internet')  
  else  
  if (Flags and INTERNET_CONNECTION_LAN) <> 0 then  
   ShowMessage('Há conexão com a Internet através de um roteador')  
  else  
  if (Flags and INTERNET_CONNECTION_PROXY) <> 0 then  
   ShowMessage('Há conexão com a Internet através de um proxy')  
  else  
   ShowMessage('Há conexão com a Internet');  
 end;  


Conclusões

Notamos neste artigo a facilidade em verificar se há conexão com a
Internet utilizando a ferramente Delphi 7. Forte abraço e até o
próximo tutorial.

--

terça-feira, 3 de abril de 2012

Validar E-mail no Delphi 7


Veja neste artigo como criar uma função no Delphi para validar um e-mail digitado, sempre lembrando que tal e-mail deve conter @ (arroba) e . (ponto).



Abaixo está a função para validar o campo:

 function ValidaEmail(aStr : String) : Boolean;  
 begin  
  aStr := Trim(UpperCase(aStr));  
  if Pos('@', aStr) &gt; 1 then  
  begin  
   Delete(aStr, 1, pos('@', aStr));  
   Result := (Length(aStr) &gt; 0) and (Pos('.', aStr) &gt; 2);  
  end  
  else  
   Result := False;  
 end;  




Para finalizar basta colocar seu componente
onde o e-mail foi digitado como parâmetro:



 if ValidaEmail(Edit1.Text) then  


Conclusões



Notamos neste artigo a facilidade em se validar
e-mail pelo Delphi e o quanto isso nos pode ser
útil no dia-a-dia. Forte abraço e até o próximo
tutorial.

--