?? 110302_05.htm
字號:
<html>
<head>
<link REL="stylesheet" HREF="/style.css" TYPE="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>計算機世界日報:
增強Delphi的TStatusBar控件
</title>
<!--***********-->
</head>
<body bgcolor="#FFFFFF" >
<p><br>
<font color="#b904e8"><b><em><a href="class.htm">返回</a></em></b></font>
<center>
<font color="#0000c0"><h2>
增強Delphi的TStatusBar控件
</h2></font>
<h3>
西安交通大學
<br>劉明華
</h3>
</center>
<p><font color="#ffffff">----</font>
在Office 2000、Internet Explorer以及Netscape 等軟件中,我們可以看到非常漂亮的窗口狀態條。特別是Netscape Communicator,在它的窗口狀態條中不僅顯示有文字,而且還有圖形、動畫、浮動按鈕以及進度條。對于Delphi編程人員來說,為一個窗口創建狀態條是非常容易的事情,Delphi所提供的TStatusBar可視化控件可以讓我們快速地實現狀態條。然而令人遺憾的是用TStatusBar創建的狀態條僅能顯示文字。本文向大家介紹如何在Delphi程序中創建更為漂亮的StatusBar。
<p><font color="#ffffff">----</font>
要創建類似Netscape風格的狀態條,現有的Delphi控件是無能為力的了。為了讓窗口狀態條能包含非文本內容,我們需要對現有的TStatusBar控件加以改進。在TStatusBar控件的基礎上,我們編寫一個新的Delphi控件TStatusBarEx。大家知道,Delphi的TStatusBar控件是不能接受其它控件的,所以我們不可能將一個TImage、TButton等放在TStatusBar上。但是我們接下來要創建的TStatusBarEx控件將可以包容其它的控件。通過TStatusBarEx控件,我們可以使Delphi創建的狀態條跟Netscape的狀態條一樣漂亮,讓其可以包含圖形、動畫、進度條等等。
<p><font color="#ffffff">----</font>
在Delphi中,一個控件上能否成為其它控件的父控件取決于此控件的ControlStyle屬性。ControlStyle屬性是集合類型的,如果此集合包含csAcceptsControls元素,則它能接受其它控件;否則,它就不能成為其它控件的父控件。ControlStyle屬性只能在控件的構造函數(Constructor)中指定,在程序運行時它是不能被改變的。所以如果希望窗口狀態條上面能包含其它控件,我們只需要在繼承類中重載TStatusBar控件的Constructor函數,并且讓控件的集合屬性ControlStyle中包含csAcceptsControls即可。
TStatusBarEx控件的實現
<p><font color="#ffffff">----</font>
以下是實現TStatusBarEx控件的Delphi源代碼,請把這段代碼拷貝下來,并且將其保存到文件StatusBarEx.PAS中去。然后用Delphi打開StatusBarEx.PAS文件,之后選擇“Component | Install Component …”,將TStatusBarEx控件安裝。
<pre>
//文件名: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中的成員函數的實現
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.
</pre>
<p><font color="#ffffff">----</font>
使用TStatusBarEx控件,我們可以非常容易地在StatusBar上增添其它的內容了。比如,如果想要在狀態條上顯示一個圖片,只要在TStatusBarEx控件上放一個Image控件;想要添加一個進度條,只需在上面加一個ProgressBar就行了!
<p align="right"><small><em>中國計算機世界出版服務公司版權所有</em></small>
<br>
</p>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -