Delphi InputBox、InputQuery函数 - 提示输入信息框

Delphi  InputBox、InputQuery函数 - 提示输入信息框

1、InputQuery

原型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function InputQuery(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);
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := APrompt;
        Left := MulDiv(8, DialogUnits.X, 4);
        Top := MulDiv(8, DialogUnits.Y, 8);
        Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
        WordWrap := True;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := MulDiv(164, DialogUnits.X, 4);
        MaxLength := 255;
        Text := Value;
        SelectAll;
      end;
      ButtonTop := Edit.Top + Edit.Height + 15;
      ButtonWidth := MulDiv(50, DialogUnits.X, 4);
      ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := SMsgDlgOK;
        ModalResult := mrOk;
        Default := True;
        SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := SMsgDlgCancel;
        ModalResult := mrCancel;
        Cancel := True;
        SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15,
          ButtonWidth, ButtonHeight);
        Form.ClientHeight := Top + Height + 13;         
      end;
      if ShowModal = mrOk then
      begin
        Value := Edit.Text;
        Result := True;
      end;
    finally
      Form.Free;
    end;
end;

返回值:

  InputQuery函数的返回值即点击了OK返回True,否则返回False

InputQuery函数原码中:

注意这两个:

  ModalResult := mrOk;

  ModalResult := mrCancel;

这两个和窗体创建的时候采用的方法:ShowModal 相关,可以采用这种方法判断:

if ShowModal = mrOk then  或者  if ShowModal = mrCancel then ;

 

2、InputBox 的函数

原型(调用了InputQuery 函数,InputQuery它才是鼻祖):

1
2
3
4
5
function InputBox(const ACaption, APrompt, ADefault: string): string; -- 输出文本
begin
 Result := ADefault;
 InputQuery(ACaption, APrompt, Result);
end;

示例:

1
Edit1.Text:= InputBox('窗口的标题', '提示信息', '默认值');

  

3、Delphi XE 中已经优化了 InputQuery  让其支持数组字符串(暂不支持手机端调用) 

原型(单元:FMX.Dialogs):

1
2
3
4
5
6
7
8
9
10
function InputQuery(const ACaption: string; const APrompts: array of string; var AValues: array of string;
  const ACloseQueryFunc: TInputCloseQueryFunc): Boolean;
var
  DialogSvc: IFMXDialogService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXDialogService, DialogSvc) then
    Result := DialogSvc.InputQuery(Translate(ACaption), APrompts, AValues, ACloseQueryFunc)
  else
    Result := False;
end;

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var
  sStr:array of string;
begin
  SetLength(sStr,2);
  if InputQuery('请登录',[Chr(0)+'账 号:',Chr(0)+'密 码:'],sStr,
    function (const Values: array of string): Boolean
    begin
      Result := False;
      if Values[0]<>'滔Roy' then begin ShowMessage('账号不正确'); exit; end;
      if Values[1]<>'123456' then begin ShowMessage('密码不正确');Exit; end;
      Result := True;
     end)
  then begin
    ShowMessage('登录成功!');
  end;
end;

 

  

  

 

 

创建时间:2019.12.18   更新时间:2019.12.19、2021.12.21

posted on   滔Roy  阅读(1933)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报

导航

点击右上角即可分享
微信分享提示