使用 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. Intent的setFlag和addFlag有什么区别?

    Intent的setFlag和addFlag有什么区别?setFlag是把之前的替换掉,addFlag是添加新的 Intent it=new Intent(); it.setClass(Setting ...

  2. Python笔记(十四)_永久存储pickle

    pickle模块:将所有的Python对象转换成二进制文件存放 应用场景:编程时最好将大对象(列表.字典.集合等)用pickle写成永久数据包供程序调用,而不是直接写入程序 写入过程:将list转换为 ...

  3. 阅读笔记06-架构师必备最全SQL优化方案(2)

    四.基础优化 1.优化思路? 定位问题点吮吸:硬件-->系统-->应用-->数据库-->架构(高可用.读写分离.分库分表). 处理方向:明确优化目标.性能和安全的折中.防患未然 ...

  4. GD Library extension not available

    在后台文章上传封面时,遇到了这样一个错误 GD Library extension not available with this PHP installation Ubuntu Nginx 自己在本 ...

  5. js 函数 写法

    // function ckeckName(){}; // function checkUser(){}; // function checkPassWorld(){}; // var checkNa ...

  6. Arrays工具类使用与源码分析(1)

    Arrays工具类主要是方便数组操作的,学习好该类可以让我们在编程过程中轻松解决数组相关的问题,简化代码的开发. Arrays类有一个私有的构造函数,没有对外提供实例化的方法,因此无法实例化对象.因为 ...

  7. JAVA线程同步通信

    以下讲解Lock线程同步通信,也是比synchronized强大的一个功能点 先看一个常规的案例: 用户类 public class Person { public void eat(){ for(i ...

  8. spring-第二篇ApplicationContext国际化及事件机制

    1.ApplicationContext接口获取spring容器      ApplicationContext是BeanFactory接口的子接口,BeanFactory的常用实现类是Default ...

  9. python学习第三十六天命名空间的概念

    python命名空间也叫名字空间,也叫名称空间,任何编程语言都有命名空间,大体意思都一样,定义文件所在的目录,下面详细讲述命名空间几种情况 1,locals: 是函数内的名称空间,包括局部变量和形参 ...

  10. C# 引用类型的深度拷贝帮助类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Lin ...