利用Delphi实现支持窗体的自定义函数
点击次数:59 次 发布日期:2008-11-09 08:39:53 作者:源代码网
|
源代码网推荐 源代码网推荐 ---- 第一步,建立函数的操作窗体; 源代码网推荐 源代码网推荐 ---- 在Delphi建立一空白窗体(form),然后加入如下控件,两个按钮Button与一个标签Label。 源代码网推荐 源代码网推荐 Button1: TButton; Label1: TLabel; Button2: TButton; 源代码网推荐 ---- 第二步,建立输出函数; 源代码网推荐 ---- 此函数为其他窗体具体调用的格式,我们就通过此函数来传递参数及返回窗体的操作结果。其形式为: 源代码网推荐 源代码网推荐 ---- function ShowMsg(btn1, btn2, msg, titleMsg: string): Integer; 源代码网推荐 源代码网推荐 ---- 其中,btn1,btn2准备接收两个按钮的标题,msg为显示的提示信息,titleMsg为窗体的标明,并且,此函数的返回值为integer. 源代码网推荐 源代码网推荐 ---- 注意:此声明必须放在implementation语句之前,使其对外可见。 源代码网推荐 源代码网推荐 ---- 第三步,实现输出函数; 源代码网推荐 源代码网推荐 ---- 具体代码如下: 源代码网推荐 源代码网推荐 function 源代码网推荐 ShowMsg(btn1, btn2, msg, titleMsg: string): Integer; var frmshowMsg: 源代码网推荐 TfrmShowMsg; begin frmShowMsg := TfrmShowMsg.Create(Application); //建立自定义函数使用的窗体 源代码网推荐 with frmShowMsg do try button1.Caption := btn1; button2.Caption := btn2; 源代码网推荐 Label1.Caption := msg; Caption := titleMsg; if ShowModal=mrOK then//显示窗体 源代码网推荐 result:=1 else result:=2; finally Free;//清除不再使用的窗体 end; end; 源代码网推荐 ---- 作为例子,只是最简单的功能实现。 源代码网推荐 ---- 最后一步,删除建立此窗体时生成的代码; 源代码网推荐 源代码网推荐 var frmShowMsg:TfrmShowMsg; 源代码网推荐 ---- 下面让我们看看如何使用上面实现的自定义函数。 源代码网推荐 ---- 建立一个应用,通过delphi->File->New Application即可。 源代码网推荐 源代码网推荐 ---- 然后,在此应用中加入刚才实现的单元文件。此时我们需要在Project Options中设定frmShowMsg为可用的窗体。用一个按钮测试一下这个自定义函数ShowMsg。showMsg("正确","取消","真的退出程序吗?","确认"); 源代码网推荐 源代码网推荐 ---- 运行,就可以看到支持窗体的自定义函数了,效果与Delphi中Application.MessageBox差不多。 源代码网推荐 源代码网推荐 ---- 掌握此种实现方法后,我们就可以继续构造出需多类似的通用函数,以完成不同的功能,如支持数据浏览的自定义函数,查看任意数据表等等。 源代码网推荐 源代码网推荐 ---- 最后附完整的此自定义函数的程序清单: 源代码网推荐 源代码网推荐 unit udfForm; 源代码网推荐 interface 源代码网推荐 uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, 源代码网推荐 Dialogs, StdCtrls; 源代码网推荐 type TfrmShowMsg = class(TForm) 源代码网推荐 Button1: TButton; 源代码网推荐 Label1: TLabel; 源代码网推荐 Button2: TButton; 源代码网推荐 private 源代码网推荐 { Private declarations } 源代码网推荐 public 源代码网推荐 { Public declarations } 源代码网推荐 end; 源代码网推荐 function ShowMsg(btn1, btn2, msg, titleMsg: string): Integer; 源代码网推荐 implementation 源代码网推荐 {$R *.DFM} 源代码网推荐 function ShowMsg(btn1, btn2, msg, titleMsg: string): Integer; 源代码网推荐 var 源代码网推荐 frmshowMsg: TfrmShowMsg; 源代码网推荐 begin 源代码网推荐 frmShowMsg := TfrmShowMsg.Create(Application); 源代码网推荐 with frmShowMsg do 源代码网推荐 try 源代码网推荐 button1.Caption := btn1; 源代码网推荐 button2.Caption := btn2; 源代码网推荐 Label1.Caption := msg; 源代码网推荐 Caption := titleMsg; 源代码网推荐 if ShowModal=mrOK 源代码网推荐 then result:=1 源代码网推荐 else result:=2; 源代码网推荐 finally Free; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 end. 源代码网推荐 源代码网供稿. |
