使用 InputBox、InputQuery 的启发

看了 InputBox、InputQuery 函数实现的源码, 有些收获与心得...

--------------------------------------------------------------------------------

通过 InputBox 可获取用户输入的字符串:

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

var

str: string;

begin

str := InputBox('输入窗口标题', '输入提示', '默认输入内容');

ShowMessage(str); //显示输入的内容

end;

--------------------------------------------------------------------------------

InputBox 是调用了 InputQuery, InputQuery 是通过一个 var 参数获取新字串:

--------------------------------------------------------------------------------

procedure TForm1.Button2Click(Sender: TObject);

var

str: string;

begin

InputQuery('输入窗口标题', '输入提示', str);

ShowMessage(str); //显示输入的内容

end;

--------------------------------------------------------------------------------

InputQuery 可返回一个 Boolean 值, 可判断用户是确认还是取消, 挺有用的:

--------------------------------------------------------------------------------

procedure TForm1.Button3Click(Sender: TObject);

var

str: string;

begin

str := '默认输入内容';

if InputQuery('输入窗口标题', '输入提示', str) then

ShowMessage(str); //如果点击了 ok 按钮将显示输入的内容

end;

--------------------------------------------------------------------------------

我经常用到需要用户输入或选择的窗口, 一般都是 Show 一个提前设计好的窗口; 这会浪费资源, InputQuery 的窗口应该是动态建立的.

但如果动态建立还是有诸多麻烦, 譬如给按钮添加事件...

但查看 InputQuery 的源码, 只有区区几十行, 果然是动态建立的窗口, 但并没有发现 "事件" 相关的代码!

再看下去, 发现这和 "模式窗口" 有关, 假如不是用 ShowModal 启动窗口的话, 恐怕 "事件" 代码是非写不可.

那 "模式窗口" 中的 "按钮" 和 "模式窗口" 又是如何关联的呢?

还是做起来看吧:

--------------------------------------------------------------------------------

//主体代码就这些:

procedure TForm1.Button1Click(Sender: TObject);

var

frm: TForm;

begin

frm := TForm.Create(Application);

frm.ShowModal;

frm.Free;

end;

--------------------------------------------------------------------------------

同时动态添加进两个按钮:

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

var

frm: TForm;

begin

frm := TForm.Create(Application);

with TButton.Create(frm) do begin

Caption := '确认';

Parent := frm;

Left := 100; top := 40;

end;

with TButton.Create(frm) do begin

Caption := '取消';

Parent := frm;

Left := 100; top := 80;

end;

frm.ShowModal;

frm.Free;

end;

--------------------------------------------------------------------------------

但上面代码写出的按钮并没有任何作用, 其实只要设置一下按钮的 ModalResult 属性就可以了:

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

var

frm: TForm;

begin

frm := TForm.Create(Application);

with TButton.Create(frm) do begin

Caption := '确认';

ModalResult := mrOk; {1}

Parent := frm;

Left := 100; top := 40;

end;

with TButton.Create(frm) do begin

Caption := '取消';

ModalResult := mrCancel; {2}

Parent := frm;

Left := 100; top := 80;

end;

frm.ShowModal;

frm.Free;

end;

--------------------------------------------------------------------------------

按钮的 ModalResult 属性就是一个整数, 窗体也有相同的属性; 点击按钮时它会传递给所属窗体的同名(ModalResult)属性; 这个属性的默认值是 0, 非 0 时模式窗口即刻关闭.

另外: ShowModal 是个函数, 调用时会返回窗口的 ModalResult 值.

上面几句话挺重要, 我也都从相关源码中核实了.

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

var

frm: TForm;

begin

frm := TForm.Create(Application);

with TButton.Create(frm) do begin

Caption := '确认';

ModalResult := mrOk; {1}

Parent := frm;

Left := 100; top := 40;

end;

with TButton.Create(frm) do begin

Caption := '取消';

ModalResult := mrCancel; {2}

Parent := frm;

Left := 100; top := 80;

end;

if frm.ShowModal = mrOk then

ShowMessage('确认了')

else

ShowMessage('没有确认');

frm.Free;

end;

--------------------------------------------------------------------------------

完善一下, 并添加两个文本框准备接受信息:

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

var

frm: TForm;

begin

frm := TForm.Create(Application);

frm.Position := poScreenCenter;

frm.ClientWidth := 270;

frm.ClientHeight := 100;

frm.Caption := '输入对话框';

with TEdit.Create(frm) do begin

Parent := frm;

Name := 'Edit1';

SetBounds(10, 10, 100, 20);

end;

with TEdit.Create(frm) do begin

Parent := frm;

Name := 'Edit2';

SetBounds(150, 10, 100, 20);

end;

with TButton.Create(frm) do begin

Caption := '确认';

ModalResult := mrOk;

Default := True;

Parent := frm;

SetBounds(100, 40, 75, 25);

end;

with TButton.Create(frm) do begin

Caption := '取消';

ModalResult := mrCancel;

Cancel := True;

Parent := frm;

SetBounds(100, 70, 75, 25);

end;

if frm.ShowModal = mrOk then

begin

ShowMessage('确认了');

end;

frm.Free;

end;

--------------------------------------------------------------------------------

如何传回用户输入的信息呢? 我觉得用类似 InputQuery 函数中的 var 参数接受信息就挺好的, 这种方式用于接受复杂的信息也很方便; 不过要先把上面的代码写到一个函数里:

--------------------------------------------------------------------------------

{函数}

function GetInfo(var str1,str2: string): Boolean;

var

frm: TForm;

MyEdit1, MyEdit2: TEdit;

begin

Result := False;

frm := TForm.Create(Application);

frm.Position := poScreenCenter;

frm.ClientWidth := 270;

frm.ClientHeight := 100;

frm.Caption := '输入对话框';

MyEdit1 := TEdit.Create(frm);

with MyEdit1 do begin

Parent := frm;

Text := str1;

SetBounds(10, 10, 100, 20);

end;

MyEdit2 := TEdit.Create(frm);

with MyEdit2 do begin

Parent := frm;

Text := str2;

SetBounds(150, 10, 100, 20);

end;

with TButton.Create(frm) do begin

Caption := '确认';

ModalResult := mrOk;

Default := True;

Parent := frm;

SetBounds(100, 40, 75, 25);

end;

with TButton.Create(frm) do begin

Caption := '取消';

ModalResult := mrCancel;

Cancel := True;

Parent := frm;

SetBounds(100, 70, 75, 25);

end;

if frm.ShowModal = mrOk then

begin

str1 := MyEdit1.Text;

str2 := MyEdit2.Text;

Result := True;

end;

frm.Free;

end;

{调用测试}

procedure TForm1.Button1Click(Sender: TObject);

var

s1,s2: string;

begin

s1 := 'aaa';

s2 := 'bbb';

if GetInfo(s1, s2) then ShowMessageFmt('%s - %s', [s1, s2]);

end;

--------------------------------------------------------------------------------

完成了, 但这远远没有 InputQuery 的源码精彩; 不过从今往后再用到一个采集信息的对话框时, 我会尽量用这样一个函数来完成; 这就是我阅读 InputQuery 源码的收获.

delphi 使用 InputBox、InputQuery 的启发的更多相关文章

  1. Delphi Inputbox,InputQuery用法

    Delphi :InputQuery,InputBox用法及区别 function InputQuery(const ACaption, APrompt: string; var Value: str ...

  2. Delphi中Inputbox 和Inputquery 函数的使用

    原文转自:http://blog.csdn.net/zengcong2013/article/details/18355959 inputbox的返回值是,你在输入框中输入的文字.而inputquer ...

  3. delphi控件属性大全-详解-简介

    http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...

  4. delphi中的inpubox,如何能控制它的位置? 10

    https://zhidao.baidu.com/question/153270855.html delphi中的inpubox,如何能控制它的位置? 10 RT ! 前辈!最好你就把那代码都拿出来吧 ...

  5. [原创] Delphi InputBox、InputQuery函数

    Delphi InputBox.InputQuery函数 两个函数都是弹框提示输入信息 function InputQuery(const ACaption, APrompt: string; var ...

  6. 【转】Delphi的消息对话框

    Delphi的消息对话框 输入输出inputBox()函数MessageBox()ShowMessage 对话框是Windows操作系统中程序与用户沟通的一种常见的交互方式,对话框可以向用户提供当前程 ...

  7. delphi弹出信息框大全(转载)

    1. 警告信息框 MessageBox(Handle,'警告信息框','警告信息框',MB_ICONWARNING); 2.疑问信息框 MessageBox(Handle,'疑问信息框','疑问信息框 ...

  8. Delphi完成的断点续传例子 转

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. Delphi的Socket编程步骤(repulish)

    转贴自:http://topic.csdn.net/t/20010727/16/212155.html ClientSocket 和ServerSocket几个重要的属性:   1.client和se ...

随机推荐

  1. Delphi IdHttp组件+IdHttpServer组件实现文件下载服务

     http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013- ...

  2. 设计模式 - 门面模式(Facade Pattern,也叫外观模式)

    简介 场景 将系统划分为若干个子系统有利于降低系统的复杂性,但是这会增加调用者的复杂性.通过引入 Facade 可以对调用者屏蔽系统内部子系统的细节. Java 中有多个日志库,例如 log4j.lo ...

  3. php Function ereg() is deprecated的解决方法

    PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”.问题根源是php中有两种正则表示方法,一个是posix,一个是perl,ph ...

  4. C++ STL:优先队列的使用详解

    堆是一个很重要的数据结构,那么我们如何更加简洁的去写大根/小根堆呢? 对于很多语言来说,只能一步一步手打,但是对于C++来说,写大根小根堆就简便得多,因为C++中有一个容器叫做priority_que ...

  5. 回调-> 观察者模式->反应堆模式

    关于回调: 回调是观察者模式以及反应堆模式的基础 一句话,回调就是一种双向调用模式,什么意思呢,就是说,被调用方在被调用时也会调用对方,这就叫回调.“If you call me, i will ca ...

  6. Java中的I/O

    1.Java中的I/O分类 I/O分类: 输入流,输出流,或者字节流,字符流 I/O中的四个抽象基类: InputStream,OutputStream:两者属于字节流,前者输入,后者输出.一般后缀名 ...

  7. seaborn教程4——分类数据可视化

    https://segmentfault.com/a/1190000015310299 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风格设置 绘 ...

  8. HDU 1255 覆盖的面积 ( 扫描线 + 离散 求矩阵大于k次面积并 )

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. php cookie session 深究一下

    当一个用户用浏览器访问web(www.96net.com.cn)时候,若服务器开启session_start() 服务器tmp临时目录 自动生成session_id 并放回给创建一个cookie 保存 ...

  10. Mybatis-技术专区-Mapper接口以及Example的实例函数及详解

    一.mapper接口中的方法解析 mapper接口中的函数及方法 int countByExample(UserExample example) thorws SQLException     按条件 ...