在Office 2000、Internet Explorer以及Netscape 等軟件中,我們可以看到非常漂亮的窗口狀態(tài)條。特別是Netscape Communicator,在它的窗口狀態(tài)條中不僅顯示有文字,而且還有圖形、動(dòng)畫、浮動(dòng)按鈕以及進(jìn)度條。對于Delphi編程人員來說,為一個(gè)窗口創(chuàng)建狀態(tài)條是非常容易的事情,Delphi所提供的TStatusBar可視化控件可以讓我們快速地實(shí)現(xiàn)狀態(tài)條。然而令人遺憾的是用TStatusBar創(chuàng)建的狀態(tài)條僅能顯示文字。本文向大家介紹如何在Delphi程序中創(chuàng)建更為漂亮的StatusBar。
---- 要?jiǎng)?chuàng)建類似Netscape風(fēng)格的狀態(tài)條,現(xiàn)有的Delphi控件是無能為力的了。為了讓窗口狀態(tài)條能包含非文本內(nèi)容,我們需要對現(xiàn)有的TStatusBar控件加以改進(jìn)。在TStatusBar控件的基礎(chǔ)上,我們編寫一個(gè)新的Delphi控件TStatusBarEx。大家知道,Delphi的TStatusBar控件是不能接受其它控件的,所以我們不可能將一個(gè)TImage、TButton等放在TStatusBar上。但是我們接下來要?jiǎng)?chuàng)建的TStatusBarEx控件將可以包容其它的控件。通過TStatusBarEx控件,我們可以使Delphi創(chuàng)建的狀態(tài)條跟Netscape的狀態(tài)條一樣漂亮,讓其可以包含圖形、動(dòng)畫、進(jìn)度條等等。
---- 在Delphi中,一個(gè)控件上能否成為其它控件的父控件取決于此控件的ControlStyle屬性。ControlStyle屬性是集合類型的,如果此集合包含csAcceptsControls元素,則它能接受其它控件;否則,它就不能成為其它控件的父控件。ControlStyle屬性只能在控件的構(gòu)造函數(shù)(Constructor)中指定,在程序運(yùn)行時(shí)它是不能被改變的。所以如果希望窗口狀態(tài)條上面能包含其它控件,我們只需要在繼承類中重載TStatusBar控件的Constructor函數(shù),并且讓控件的集合屬性ControlStyle中包含csAcceptsControls即可。 TStatusBarEx控件的實(shí)現(xiàn)
---- 以下是實(shí)現(xiàn)TStatusBarEx控件的Delphi源代碼,請把這段代碼拷貝下來,并且將其保存到文件StatusBarEx.PAS中去。然后用Delphi打開StatusBarEx.PAS文件,之后選擇“Component | Install Component …”,將TStatusBarEx控件安裝。
//文件名:StatusBarEx.pas unit StatusBarEx;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, DsgnIntf;
type
//定義About屬性的屬性編輯器 TAbout = class(TPropertyEditor) public procedure Edit; override; function GetAttributes: TPropertyAttributes; override; function GetValue: string; override; end;
//定義TStatusBarEx控件 TStatusBarEx = class(TStatusBar) private { Private declarations } FAbout:TAbout; protected { Protected declarations } public { Public declarations } constructor Create(AOwner: TComponent); override; published { Published declarations } property About: TAbout read FAbout; end;
procedure Register;
implementation
constructor TStatusBarEx.Create(AOwner: TComponent); begin inherited Create(AOwner);
{為了讓TStatusBarEx控件能接受其它控件,必須 使ControlStyle屬性(集合類型)包含csAcceptsControls元素} ControlStyle:= ControlStyle + [csAcceptsControls]; end;
//以下是TAbout中的成員函數(shù)的實(shí)現(xiàn) procedure TAbout.Edit; begin Application.MessageBox('TStatusBarEx for Delphi 5'#13#10 +'Written by Simon Liu'#13#10 +'Email:simon_liu@263.net', 'About TStatusBarEx',MB_ICONINFORMATION); end;
function TAbout.GetAttributes: TPropertyAttributes; begin Result := [paDialog, paReadOnly]; end;
function TAbout.GetValue: string; begin Result := '(Simon)'; end;
procedure Register; begin //將TStatusBarEx控件注冊到Delphi 5控件板的Win32頁上 RegisterComponents('Win32', [TStatusBarEx]);
//為About屬性注冊屬性編輯器 RegisterPropertyEditor(typeInfo(TAbout), TStatusBar, 'About', TAbout); end;
end.
---- 使用TStatusBarEx控件,我們可以非常容易地在StatusBar上增添其它的內(nèi)容了。比如,如果想要在狀態(tài)條上顯示一個(gè)圖片,只要在TStatusBarEx控件上放一個(gè)Image控件;想要添加一個(gè)進(jìn)度條,只需在上面加一個(gè)ProgressBar就行了!
|