如何在Delphi程序中创建更为漂亮的StatusBar?

---- 要创建类似Netscape风格的状态条,现有的Delphi控件是无能为力的了。为了让窗口状态条能包含非文本内容,我们需要对现有的TStatusBar控件加以改进。在TStatusBar控件的基础上,我们编写一个新的Delphi控件TStatusBarEx。大家知道,Delphi的TStatusBar控件是不能接受其它控件的,所以我们不可能将一个TImage、TButton等放在TStatusBar上。但是我们接下来要创建的TStatusBarEx控件将可以包容其它的控件。通过TStatusBarEx控件,我们可以使Delphi创建的状态条跟Netscape的状态条一样漂亮,让其可以包含图形、动画、进度条等等。

---- 在Delphi中,一个控件上能否成为其它控件的父控件取决于此控件的ControlStyle属性。ControlStyle属性是集合类型的,如果此集合包含csAcceptsControls元素,则它能接受其它控件;否则,它就不能成为其它控件的父控件。ControlStyle属性只能在控件的构造函数(Constructor)中指定,在程序运行时它是不能被改变的。所以如果希望窗口状态条上面能包含其它控件,我们只需要在继承类中重载TStatusBar控件的Constructor函数,并且让控件的集合属性ControlStyle中包含csAcceptsControls即可。 TStatusBarEx控件的实现

---- 以下是实现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中的成员函数的实现
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上增添其它的内容了。比如,如果想要在状态条上显示一个图片,只要在TStatusBarEx控件上放一个Image控件;想要添加一个进度条,只需在上面加一个ProgressBar就行了!

转载于:https://www.cnblogs.com/myamanda/articles/1528747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值