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

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

1、InputQuery

原型:

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它才是鼻祖):

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

示例:

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

  

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

原型(单元:FMX.Dialogs):

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;

示例:

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 2019-12-18 17:24  滔Roy  阅读(1751)  评论(0编辑  收藏  举报

导航