【问题标题】:Delphi InputBox for password entry?Delphi InputBox 用于输入密码?
【发布时间】:2010-10-10 03:10:06
【问题描述】:

输入框:

answer:=Inputbox('a','b','c');

效果很好,但我正在寻找一个蒙面的,就像一个密码框,你只能看到小星星而不是输入的字符。

【问题讨论】:

    标签: delphi mask textinput inputbox


    【解决方案1】:

    在 XE2 中,InputBox()InputQuery() 已更新为原生支持屏蔽 TEdit 输入,尽管该功能尚未记录在案。如果APrompt 参数的第一个字符设置为任何值#32,则TEdit.PasswordChar 将设置为*,例如:

    answer := InputBox('a', #31'b', 'c');
    

    【讨论】:

    • 这很直观!
    • 他们费力地扩展InputQuery()InputBox() 在内部使用)为多提示和OnCloseQuery 回调提供了新参数,但他们懒得创建一个用于指定密码屏蔽的新参数?多提示支持按提示屏蔽,但它们可以使该界面更直观。他们应该使用包含字符串/掩码对的记录数组,而不是使用带有特殊前导字符的字符串数组。这甚至允许添加未来的字段以更好地控制TEdit 控件。但是没有。
    【解决方案2】:

    您可以向InputBox 创建的编辑控件发送一条 Windows 消息,该消息将标记该编辑控件以供输入密码。以下代码取自http://www.swissdelphicenter.ch/en/showcode.php?id=1208

    const
       InputBoxMessage = WM_USER + 200;
    
    type
       TForm1 = class(TForm)
         Button1: TButton;
         procedure Button1Click(Sender: TObject);
       private
         procedure InputBoxSetPasswordChar(var Msg: TMessage); message InputBoxMessage;
       public
       end;
    
    var
       Form1: TForm1;
    
    implementation
    
    {$R *.DFM}
    
    procedure TForm1.InputBoxSetPasswordChar(var Msg: TMessage);
    var
       hInputForm, hEdit, hButton: HWND;
    begin
       hInputForm := Screen.Forms[0].Handle;
       if (hInputForm <> 0) then
       begin
         hEdit := FindWindowEx(hInputForm, 0, 'TEdit', nil);
         {
           // Change button text:
           hButton := FindWindowEx(hInputForm, 0, 'TButton', nil);
           SendMessage(hButton, WM_SETTEXT, 0, Integer(PChar('Cancel')));
         }
         SendMessage(hEdit, EM_SETPASSWORDCHAR, Ord('*'), 0);
       end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
       InputString: string;
    begin
       PostMessage(Handle, InputBoxMessage, 0, 0);
       InputString := InputBox('Input Box', 'Please Enter a Password', '');
    end;
    

    【讨论】:

    • 哇!推迟执行的优雅方式:-)
    • 这可能是一个转储问题,但你怎么知道Screen.Forms[0]InputBox 创建的表单?
    【解决方案3】:

    InputBox 调用 Dialogs 中的 InputQuery 函数,动态创建表单。您可以随时复制此函数并更改 TEdit 的 PasswordChar 属性。

    【讨论】:

      【解决方案4】:

      我认为 Delphi 没有开箱即用的功能。也许您可以在http://www.torry.net/ 或网络上的其他地方找到一个。否则自己写一个——应该不会那么难。 :-) 如果您有“足够大”的 Delphi 版本,您甚至可以查看源代码。

      乌力。

      【讨论】:

        【解决方案5】:

        您可以使用 InputQuery 代替 InputBox。当设置 TRUE 参数时,密码字段将被屏蔽。

        InputQuery('Authenticate', 'Password:',TRUE, value);     
        

        这里有一些资源; http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/inputquery.html

        【讨论】:

          【解决方案6】:

          如果有人仍然需要一个简单的解决方案,这里是:

          InputQuery('MyCaption', #0 + 'MyPrompt', Value); // <-- the password char '*' is used
          

          之所以可行,是因为 InputQuery 函数具有以下嵌套函数:

          function GetPasswordChar(const ACaption: string): Char;
          begin
            if (Length(ACaption) > 1) and (ACaption[1] < #32) then
              Result := '*'
            else
              Result := #0;
          end;
          

          每个提示都会调用它:

          PasswordChar := GetPasswordChar(APrompts[I]);
          

          因此,如果 APrompts 中的第一个字符是

          在 Delphi 10.4 上测试过。我不确定这是什么时候引入的,我直接从 D6 跳到 10.4,还没有在 D6 上测试过。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-26
            • 1970-01-01
            • 1970-01-01
            • 2012-12-17
            • 1970-01-01
            • 2014-02-12
            相关资源
            最近更新 更多