应为statusbar的controlstyle不允许其作为容器,如果非要用的话只能继承下来一个新组建,比如type TMStatusBar=Class(TStatusBar) private protected public constructor Create(AOwner: TComponent); override; published end;//TMStatusBar 事件扩展 constructor TMStatusBar.Create(AOwner: TComponent); begin inherited Create(AOwner); ControlStyle:= ControlStyle + [csAcceptsControls]; end;
uses CommCtrl;type THackControl = class(TControl);procedure TForm1.FormCreate(Sender: TObject); var PanelRect: TRect; begin THackControl(ProgressBar1).SetParent(StatusBar1); SendMessage(StatusBar1.Handle, SB_GETRECT, 1, Integer(@PanelRect)); with PanelRect do ProgressBar1.SetBounds(Left, Top, Right-Left, Bottom-Top); end;procedure TForm1.Button1Click(Sender: TObject); var i: integer; begin with ProgressBar1 do for i := 0 to 99 do begin Position := Position + 1; Sleep(10); end; end;説明:設定二個StatusPanel、把第二個做為进度条。
Panel: TStatusPanel; const Rect: TRect);
begin
Gauge1.Parent:=StatusBar1;
Gauge1.Left:=Rect.Left;
Gauge1.Top:=Rect.Top;
Gauge1.Width:=Panel.Width;
Gauge1.Height:=Rect.Bottom-Rect.Top;
end;
Gauge1 为进度条,在statusbar1的DrawPanel事件中写
示例,新建一个form1,添加statusbar1,双击statusbar1添加两个statuspanel,第一个设宽度100,再添加一个prograssbar1,保存为unit1.pas
打开对应的unit1.dfm,看到如下结构
object StatusBar1: TStatusBar
Left = 0
Top = 434
Width = 688
Height = 19
Panels = <
item
Width = 100
end
item
Width = 50
end>
end
object ProgressBar1: TProgressBar
Left = 32
Top = 400
Width = 150
Height = 16
TabOrder = 1
end将其改为
object StatusBar1: TStatusBar
Left = 0
Top = 434
Width = 688
Height = 19
Panels = <
item
Width = 100
end
item
Width = 50
end>
object ProgressBar1: TProgressBar
Left = 0
Top = 3
Width = 100
Height = 16
TabOrder = 0
end
end保存,关闭之后再打开刚刚的工程,就可以了
设置新的panels[0]的style属性为psownerdraw;
才能显示文字以外的东东.////切记!!!!
TMStatusBar=Class(TStatusBar)
private
protected
public
constructor Create(AOwner: TComponent); override;
published
end;//TMStatusBar 事件扩展 constructor TMStatusBar.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle:= ControlStyle + [csAcceptsControls];
end;
var
PanelRect: TRect;
begin
THackControl(ProgressBar1).SetParent(StatusBar1);
SendMessage(StatusBar1.Handle, SB_GETRECT, 1, Integer(@PanelRect));
with PanelRect do
ProgressBar1.SetBounds(Left, Top, Right-Left, Bottom-Top);
end;procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
with ProgressBar1 do
for i := 0 to 99 do
begin
Position := Position + 1;
Sleep(10);
end;
end;説明:設定二個StatusPanel、把第二個做為进度条。
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private }
public
{ Public }
end;