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

发布于 2024-11-15 13:17:43 字数 205 浏览 3 评论 0原文

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

这可能吗?

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

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

I have an inputbox and would like the user to enter a password, but at the same time hide it.

Is this possible?

This is my code so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

在巴黎塔顶看东京樱花 2024-11-22 13:17:43

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

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

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

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;

You 'cannot' use InputBox for this, because, well... clearly this function doesn't hide the text.

The standard Windows edit control has a 'password mode', though. To test this, simply add a TEdit to a form and set its PasswordChar to *.

If you want to use such an edit in an input box, you have to write this dialog yourself, like my 'super input dialog':

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;

Try it:

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

瑕疵 2024-11-22 13:17:43

这看起来像是在这里得到了回答:

Delphi InputBox 用于密码输入?

This looks like it was answered here:

Delphi InputBox for password entry?

ゝ偶尔ゞ 2024-11-22 13:17:43

不要使用InputBox。自己创建一个对话框,并确保将 TEdit.PasswordChar 设置为 #0 以外的其他值。

也可以通过 Windows 消息获取 InputBox 的编辑控件的句柄并设置 PasswordChar,但我不知道如何做到这一点(特别是因为InputBox 是一个阻塞调用)。

Delphi XE 还有一个密码对话框 表单可供在创建新表单时使用。旧版本可能也是如此,XE 恰好是我现在运行的。 (编辑 Delphi 2007 也有它。2007 和 XE 是我现在安装的唯一版本的 Delphi,所以我无法验证任何其他版本。)

Don't use an InputBox. Create a dialog yourself and make sure to set TEdit.PasswordChar to something other than #0.

It may also be possible to get a handle to the InputBox's Edit control and set the PasswordChar via a Windows message, but I don't know how to do that off the top of my head (especially since the InputBox is a blocking call).

Delphi XE also has a Password Dialog form available to use when creating a new form. Older versions probably do too, XE just happens to be what I have running right now. (Edit Delphi 2007 also has it. 2007 & XE are the only versions of Delphi I have installed right now though, so I can't verify any other versions.)

一个人的夜不怕黑 2024-11-22 13:17:43
const
  InputBoxMessage = WM_USER + 200;
type
  TForm1 = class(TForm)
  ...
    procedure InputBoxSetPasswordChar(var Msg: TMessage); message InputBoxMessage;
    function GetPassword: String;
  ...
  end;
...
procedure TForm1.InputBoxSetPasswordChar(var Msg: TMessage);
var
  hInputForm, hEdit: HWND;
begin
  hInputForm := Screen.Forms[0].Handle;
  if (hInputForm <> 0) then
  begin
    hEdit := FindWindowEx(hInputForm, 0, 'TEdit', nil);
    SendMessage(hEdit, EM_SETPASSWORDCHAR, Ord('*'), 0);
  end;
end;

function TForm1.GetPassword: String;
begin
  PostMessage(Handle, InputBoxMessage, 0, 0);
  Result := InputBox('Title', 'Password:', '');
end;
const
  InputBoxMessage = WM_USER + 200;
type
  TForm1 = class(TForm)
  ...
    procedure InputBoxSetPasswordChar(var Msg: TMessage); message InputBoxMessage;
    function GetPassword: String;
  ...
  end;
...
procedure TForm1.InputBoxSetPasswordChar(var Msg: TMessage);
var
  hInputForm, hEdit: HWND;
begin
  hInputForm := Screen.Forms[0].Handle;
  if (hInputForm <> 0) then
  begin
    hEdit := FindWindowEx(hInputForm, 0, 'TEdit', nil);
    SendMessage(hEdit, EM_SETPASSWORDCHAR, Ord('*'), 0);
  end;
end;

function TForm1.GetPassword: String;
begin
  PostMessage(Handle, InputBoxMessage, 0, 0);
  Result := InputBox('Title', 'Password:', '');
end;
2024-11-22 13:17:43

我认为您还需要设置:

  Echomode := eemPassword

至少对于 TdlcxLabeledDBTextEdit。

I think you also need to set:

  Echomode := eemPassword

At least for TdlcxLabeledDBTextEdit.

累赘 2024-11-22 13:17:43
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if checkbox1.checked = true then
    edit1.passwordchar := '*'
   else
    edit1.PasswordChar := #0;
  end;
end;
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if checkbox1.checked = true then
    edit1.passwordchar := '*'
   else
    edit1.PasswordChar := #0;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文