p> 在使用Netscape的navigator时不知你有没有注意到:在它的状态栏有一组按钮,这使我们能很方便地打开另一个窗体,更有效地利用时间(要知道网上冲浪时间就是金钱啊!)。 接下来我要讲的是如何在我们的程序中加入此功能,我们可以用DELPHI很轻易地实现,在DELPHI中我们可以指定某个Twincontrol类(如Ttoolbar)的父级为另一个Twincontrol(这里是Tstatusbar),然后通过动态创建Ttoolbar就可以在Tstarbar上创建TToolbar了,并且为工具条(Toolbar)添加按钮及其事件,通过调用API中的winexec实现对Window的应用程序的调用。下面给出了详细的代码和注释给大家分享。 unit superbar; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ComCtrls; type TForm1 = class(TForm) StatusBar1: TStatusBar;{为窗体添加一个状态栏} procedure FormCreate(Sender: TObject); private {声明以下过程} procedure createsuperbar(sender:Tobject;Imagelist:TImagelist); {创建工具条} procedure createbutton(toolbar:TToolbar;Const ButtonCaptions: array of String); {创建工具条的按钮} procedure superbuttonclick(sender:Tobject); {为工具条按钮添加事件} public end; var Form1: TForm1; toolbar:TToolbar; implementation uses shellapi,commctrl;{注意要加入此句,否则程序运行无法通过} {$R *.DFM} procedure TForm1.createsuperbar(sender:Tobject;Imagelist:Timagelist); begin toolbar:=TToolbar.Create(self);{动态创建一个工具条} with toolbar do begin parent:=statusbar1;{定义toolbar的父类为状态栏,这样就可以创建出状态栏的工具条了} top:=1;left:=1;height:=18; buttonheight:=16;buttonwidth:=16;{定出toolbar和toolbutton的宽度长度}; flat:=true;autosize:=true; {设置toolbutton为浮动形式} images:=imagelist;{设置toolbutton的图标} {使工具条可以脱离状态栏.注意:如果你用的是DELPHI3.0需删除此两句 纯捎脈 dragkind:=dkdock; dragmode:=dmautomatic; end; end; procedure TForm1.createbutton(Toolbar:TToolbar;Const ButtonCaptions: array of String); var I,m: Integer; begin m:=0; for I := 0 to High(ButtonCaptions) do begin with TToolButton.Create(ToolBar) do begin Parent := ToolBar; Caption := ButtonCaptions[I]; onclick:=superbuttonclick;{为toolbutton增加鼠标click事件} if (ButtonCaptions[I]=‘|’) then{判断是不是分隔符} begin Style := tbsSeparator; m:=m+1; end else begin Style := tbsButton; imageindex:=i-m; end; end; end; end; {这里是响应鼠标事件,实现应用程序的打开} procedure TForm1.superbuttonclick(sender:Tobject); begin winexec(pchar((sender as TToolbutton).caption),SW_ShowNormal); {打开一个Windows的应用程序} end; procedure TForm1.FormCreate(Sender: TObject); const ExeList:array[0..2] of String=( ‘winfile.exe’, ‘|’, ‘notepad.exe’);{可以在这里加上其它应用程序的全称,也可以不要分隔符} var imagelist:Timagelist; i:integer; begin imagelist:=Timagelist.Create(self); try{加入安全代码,也可不加} for i:=0 to high(exelist) do if exelist[i] $#@60;$#@62; ‘|’ then begin ImageList_AddIcon(ImageList.Handle,ExtractIcon(Handle,PChar(ExeList[i]),0)); {为imagelist添加图标} end; createsuperbar(self,imagelist); createbutton(toolbar,exelist); finally end; end; end. 以上程序已在Pentium-MMX166 32M Windows 98 DELPHI 4.0 通过,小弟反复测试没有问题,才斗胆拿出来与大家分享,如有问题望广大Delphi迷能给小弟多提意见,在下将翘首期待你的来信。
本文来自中国程序员网站
|