扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
上面这个Form窗体中放了三个Tweblabel 控件,你不用编写一句程序,只是简单地把它们拖放到Form 上,改一下网页的地址,就可以运行了。当光标移动到文字上时,光标就会自动变成手形,用鼠标轻轻地点击一下,浏览器就会启动,它将把你带到你想去的地方。接下来将引导你一步一步地学习,使你不但要了解这个奇妙的Tweblabel控件,而且要学会如何用DELPHI进行最奇妙的设计即控件编程。
  启动DELPHI 3(或者DELPHI 4),菜单上有一个Component,用鼠标单击一下,选择New Component就会弹出一个窗口。有几样东西需要填写,先来解释一下。 
   Ancestor type:表示被继承的对象,是个下拉框,选TLabel。 
   Class Name:表示新创建的类的名字,取名为Tweblabel。 
   Palette Page:表示把Tweblabel放到控件面板上的哪个栏位上,选缺省Sample,也可以选别的,或者干脆取个新栏位名字。 
   Unit file name:选缺省。 
   Search path:选缺省。 
  然后按“Create Unit”按钮。 
  DELPHI为我们创建了这个单元,并建起了骨架。下面就是用DELPHI编写的代码。 
unit weblabel;
  interface 
  uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls; 
  type 
   Tweblabel = class(TLabel) 
   private 
   { Private declarations } 
   protected 
   { Protected declarations } 
   public 
   { Public declarations } 
   published 
   { Published declarations } 
   end; 
procedure Register;
  implementation 
  procedure Register; 
  begin 
   RegisterComponents(Samples, [Tweblabel]); 
  end; 
end.
DELPHI的Unit分为两个部分,一个是interface(界面),另一个是implementation(实现)。并且类的定义分private、protected、public、published几个,前面三个是类固有的,published则是控件特有的? 中的变量可以显示在DELPHI的控件编辑器里。
  有了DELPHI编写的控件的骨架之后,又该如何添加代码呢?我们需要做以下几件事情: 
  1.需要一个变量存放Internet主页的地址; 
  2.需要一个函数来完成调用浏览器访问Internet主页; 
  3.需要初始化这个Tweblabel,比如字体、颜色、风格使它更像一个链接; 
  4.当鼠标在Tweblabel上单击时,浏览器就会被启动。 
  具体作法如下: 
  1.定义变量 
   private 
   { Private declarations } 
  //定义一个变量存放HTTP主页的地址 
  Fhttpaddr:string; 
   //为了使控件编辑器能够修改它,则加入: 
   published 
   { Published declarations } 
   property CHttpaddr: string read Fhttpaddr write Fhttpaddr; 
   //将变量输出到控件编辑器中,名称应该为HTTPADDR,前面加C是为了方便, 
   //它将直接排列在Caption的下面,方便修改。 
  2.调用浏览器访问INTERNET主页函数 
   function ShellExecute(hWnd: HWND; Operation, FileName, Parameters, Directory: PChar; ShowCmd: Integer): HINST; stdcall; 
   参数定义: 
   hWnd: 父窗口句柄 
   Operation: 操作模式 open 或 print 
   FileName: 文件名指针 
   Parameter: 传递给执行文件的参数 
   Directory: 缺省目录 
   ShowCmd: 程序启动后的状态:(1)SW_SHOWNORMAL 正常 (2)SW_MINIMIZE 最小 (3)SW_MAXIMIZE 最大 
   不考虑错误判断,打开一个WEB页面的例子: 
   ShellExecute(handle, open, http://www.microsfto.com, nil, nil, SW_SHOWNORMAL); 
  函数ShellExecute包含在单元ShellAPI中。为了处理各种情况,我们定义了一个过程。 
   public 
   procedure ExploreWeb(handle:HWND ; page:PChar); 
   具体代码使用Robert Vivrette先生编写的程序片段。 
  procedure Tweblabel.ExploreWeb(handle:HWND ; page:PChar); 
  var 
   Returnvalue : integer; //实际调用WEB页面 
  begin 
   ReturnValue := ShellExecute(handle, open, page, nil, nil, SW_SHOWNORMAL); 
   if ReturnValue $#@60;= 32 then 
   case Returnvalue of 
   0 : 
MessageBox(handle,错误:内存溢出!,WEB页面出错信息,0); 
   ERROR_FILE_NOT_FOUND: 
MessageBox(handle,错误:文件未找到!,WEB页面出错信息,0); 
    ERROR_PATH_NOT_FOUND: 
MessageBox(handle,错误:目录错误!,WEB页面出错信息,0); 
   ERROR_BAD_FORMAT : 
MessageBox(handle,错误:EXE文件格式错误!,WEB页面出错信息,0); 
   // All other errors . See help for more ReturnValues of ShellExecute 
  else 
 MessageBox(handle,PChar(错误信息[:+IntToStr(Returnvalue)+]),WEB页面出错信息,0) 
   end 
  end; 
  3.我们知道,必须重载Create函数才能加入我们的初始化代码。 
   public 
   { Public declarations } 
   constructor Create(AOwner: TComponent); override; 
   实现部分: 
   constructor Tweblabel.Create(AOwner: TComponent); 
   begin 
  //调用父辈的CREATE 
  inherited Create(Aowner); 
   //以下是自己的初始化代码 
 //将光标设置为手型 
  Cursor:= crHandPoint; 
  //令标题=主页地址 
  chttpaddr:=http://www.nbip.net/michaeljia; 
  Caption:=chttpaddr; 
  //字体缺省大小为10 
  font.size:=10; 
  font.color:=clblue;   //字体缺省颜色为兰色 
  font.style:=[fsUnderline]; //字体缺省风格为下划线 
   end; 
  4.要想通过单击Tweblabel来启动浏览器,必须重载CLICK函数,代码如下。 
   protected 
   { Protected declarations } 
   procedure click; override; 
   实现部分: 
   procedure Tweblabel.click; 
   begin 
  inherited Click; //调用父辈的Click函数 
    ExploreWeb(parent.handle,pchar(chttpaddr)); //调用WEB页面 
end; 
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。