拖拉不拖拉机 發表於 2019-12-18 17:24:00

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

<p><strong><span style="font-size: 16px">Delphi&nbsp; InputBox、InputQuery函数&nbsp;- 提示输入信息框</span></strong></p>
<p><strong><span style="font-size: 16px">1、InputQuery</span></strong></p>
<p><strong><span style="font-size: 16px">原型:</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">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;</pre>
</div>
<p><strong><span style="font-size: 16px">返回值:</span></strong></p>
<p><span style="font-size: 16px">  InputQuery函数的返回值即点击了OK返回True,否则返回False</span></p>
<p><span style="font-size: 16px">InputQuery函数原码中:</span></p>
<p><span style="font-size: 16px">注意这两个:</span></p>
<p><span style="font-size: 16px">  ModalResult := mrOk;</span></p>
<p><span style="font-size: 16px">  ModalResult := mrCancel;</span></p>
<p><span style="font-size: 16px">这两个和窗体创建的时候采用的方法:ShowModal 相关,可以采用这种方法判断:</span></p>
<p><span style="font-size: 16px">if ShowModal = mrOk then&nbsp; 或者&nbsp;&nbsp;</span><span style="font-size: 16px">if ShowModal = mrCancel then ;</span></p>
<p>&nbsp;</p>
<p><strong><span style="font-size: 16px">2、InputBox 的函数</span></strong></p>
<p><span style="font-size: 16px">原型(调用了InputQuery 函数,InputQuery它才是鼻祖):</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">function InputBox(const ACaption, APrompt, ADefault: string): string; -- 输出文本
begin
Result := ADefault;
InputQuery(ACaption, APrompt, Result);
end;</pre>
</div>
<p>示例:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Edit1.Text:= InputBox('窗口的标题', '提示信息', '默认值');
</pre>
</div>
<p>  </p>
<p><strong>3、Delphi XE 中已经优化了 InputQuery&nbsp; 让其支持数组字符串(暂不支持手机端调用)</strong> </p>
<p>原型(单元:FMX.Dialogs):</p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">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;
</pre>
</div>
<p>示例:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">var
sStr:array of string;
begin
SetLength(sStr,2);
if InputQuery('请登录',,sStr,
    function (const Values: array of string): Boolean
    begin
      Result := False;
      if Values&lt;&gt;'滔Roy' then begin ShowMessage('账号不正确'); exit; end;
      if Values&lt;&gt;'123456' then begin ShowMessage('密码不正确');Exit; end;
      Result := True;
   end)
then begin
    ShowMessage('登录成功!');
end;
end;
</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/728850/202112/728850-20211221091925678-1094993767.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp; </p>
<p>  </p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp;</span></p>
<p><span style="color: rgba(136, 136, 136, 1); font-size: 14px">创建时间:2019.12.18   更新时间:2019.12.19、2021.12.21</span></p>

</div>
<div id="MySignature" role="contentinfo">
    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/12061027.html
頁: [1]
查看完整版本: Delphi InputBox、InputQuery函数 - 提示输入信息框