delphi - 如何隐藏编辑框中的密码文本?

标签 delphi passwords

我有一个输入框,希望用户输入密码,但同时隐藏它。

这可能吗?

这是我到目前为止的代码:

var password : string;
begin
 password := InputBox('Password: ', 'Please enter your password: ', password)
end;

最佳答案

您“不能”使用 InputBox 来实现此目的,因为,嗯......显然这个函数不会隐藏文本。

不过,标准 Windows 编辑控件具有“密码模式”。要对此进行测试,只需将 TEdit 添加到表单并将其 PasswordChar 设置为 *

如果你想在输入框中使用这样的编辑,你必须自己编写这个对话框,就像我的“ super 输入对话框”:

type
  TMultiInputBox = class
  strict private
    class var
      frm: TForm;
      lbl: TLabel;
      edt: TEdit;
      btnOK,
      btnCancel: TButton;
      shp: TShape;
      FMin, FMax: integer;
      FTitle, FText: string;
    class procedure SetupDialog;
    class procedure ValidateInput(Sender: TObject);
  public
    class function TextInputBox(AOwner: TCustomForm; const ATitle,
      AText: string; var Value: string): boolean;
    class function NumInputBox(AOwner: TCustomForm; const ATitle,
      AText: string; AMin, AMax: integer; var Value: integer): boolean;
    class function PasswordInputBox(AOwner: TCustomForm; const ATitle,
      AText: string; var Value: string): boolean;
  end;

class procedure TMultiInputBox.SetupDialog;
begin
  frm.Caption := FTitle;
  frm.Width := 512;
  frm.Position := poOwnerFormCenter;
  frm.BorderStyle := bsDialog;
  lbl := TLabel.Create(frm);
  lbl.Parent := frm;
  lbl.Left := 8;
  lbl.Top := 8;
  lbl.Width := frm.ClientWidth - 16;
  lbl.Caption := FText;
  edt := TEdit.Create(frm);
  edt.Parent := frm;
  edt.Top := lbl.Top + lbl.Height + 8;
  edt.Left := 8;
  edt.Width := frm.ClientWidth - 16;
  btnOK := TButton.Create(frm);
  btnOK.Parent := frm;
  btnOK.Default := true;
  btnOK.Caption := 'OK';
  btnOK.ModalResult := mrOk;
  btnCancel := TButton.Create(frm);
  btnCancel.Parent := frm;
  btnCancel.Cancel := true;
  btnCancel.Caption := 'Cancel';
  btnCancel.ModalResult := mrCancel;
  btnCancel.Top := edt.Top + edt.Height + 16;
  btnCancel.Left := frm.ClientWidth - btnCancel.Width - 8;
  btnOK.Top := btnCancel.Top;
  btnOK.Left := btnCancel.Left - btnOK.Width - 4;
  frm.ClientHeight := btnOK.Top + btnOK.Height + 8;
  shp := TShape.Create(frm);
  shp.Parent := frm;
  shp.Brush.Color := clWhite;
  shp.Pen.Style := psClear;
  shp.Shape := stRectangle;
  shp.Align := alTop;
  shp.Height := btnOK.Top - 8;
  shp.SendToBack;
end;

class function TMultiInputBox.TextInputBox(AOwner: TCustomForm; const ATitle,
  AText: string; var Value: string): boolean;
begin
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := false;
    edt.PasswordChar := #0;
    edt.Text := Value;
    edt.OnChange := nil;
    result := frm.ShowModal = mrOK;
    if result then Value := edt.Text;
  finally
    frm.Free;
  end;
end;

class function TMultiInputBox.PasswordInputBox(AOwner: TCustomForm;
  const ATitle, AText: string; var Value: string): boolean;
begin
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := false;
    edt.PasswordChar := '*';
    edt.Text := Value;
    edt.OnChange := nil;
    result := frm.ShowModal = mrOK;
    if result then Value := edt.Text;
  finally
    frm.Free;
  end;
end;

class procedure TMultiInputBox.ValidateInput(Sender: TObject);
var
  n: integer;
begin
  btnOK.Enabled := TryStrToInt(edt.Text, n) and InRange(n, FMin, FMax);
end;

class function TMultiInputBox.NumInputBox(AOwner: TCustomForm; const ATitle,
  AText: string; AMin, AMax: integer; var Value: integer): boolean;
begin
  FMin := AMin;
  FMax := AMax;
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := true;
    edt.PasswordChar := #0;
    edt.Text := IntToStr(value);
    edt.OnChange := ValidateInput;
    result := frm.ShowModal = mrOK;
    if result then Value := StrToInt(edt.Text);
  finally
    frm.Free;
  end;
end;

尝试一下:

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
begin
  str := '';
  if TMultiInputBox.PasswordInputBox(Self, 'Password',
    'Please enter your password:', str) then
    ShowMessageFmt('You entered %s.', [str]);
end;

Screenshot

关于delphi - 如何隐藏编辑框中的密码文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347686/

相关文章:

delphi - 将 TForm win32 应用程序转换为 Win7 小工具 (delphi)

Mysql - 无法更改或添加新密码

delphi - 删除 IDE 创建的表单变量是否安全?

html - 为某些密码输入字段禁用 Firefox 密码管理器

javascript - 如何将调整后的密码保存到浏览器?

Emacs GPG 密码超时

hash - 我可以在摘要式身份验证中使用已 MD5 编码的密码吗

delphi - 正在寻找 Delphi 7 代码来检测程序是否以管理员权限启动?

delphi - Delphi中从TWebRequest获取文件

Delphi 简单 TCP 服务器挂起。表单卡住但服务器继续管理客户端