输入框:
answer:=Inputbox('a','b','c');
效果不错,但我正在寻找一个掩码输入框,就像密码框一样,你只能看到星号而不是输入的字符。
在XE2中,InputBox()
和InputQuery()
已经更新以本地支持遮蔽TEdit
输入,尽管该功能尚未被记录。如果APrompt
参数的第一个字符设置为任何值< #32
,则TEdit.PasswordChar
将被设置为*
,例如:
answer := InputBox('a', #31'b', 'c');
您可以发送一个Windows消息到由InputBox
创建的编辑控件,这将标记该编辑控件需要输入密码。下面的代码来自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
创建的窗体? - Edwin YipInputBox调用Dialogs中的InputQuery函数,动态创建表单。您可以复制此函数并更改TEdit的PasswordChar属性。
如果有人仍然需要一个简单的解决方案,这里是:
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]);
在Delphi 10.4上进行了测试。我不确定这是什么时候引入的,我直接从D6跳到了10.4,并没有在D6上进行测试。
你可以使用InputQuery代替InputBox。当设置为TRUE时,密码字段将被掩盖。
InputQuery('Authenticate', 'Password:',TRUE, value);
这里有一些资源; http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/inputquery.html
InputQuery()
(InputBox()
内部使用的函数)的新参数,以支持多提示和OnCloseQuery
回调,但他们却无心为指定密码掩码创建一个新参数?多提示支持每个提示的掩码,但他们本可以使该界面更加直观。他们本可以使用包含字符串/掩码对的记录数组来代替使用带有特殊引导字符的字符串数组,这样甚至可以添加其他字段以更精细地控制TEdit
控件。但是没有。 - Remy Lebeau