如何在Delphi中将TProgressBar放入TStatusBar

Most applications provide an area in the application's main form, usually aligned at the bottom of a form, used to display information about the application as it runs.

大多数应用程序在应用程序的主窗体中提供一个区域,通常在窗体的底部对齐,用于在运行时显示有关该应用程序的信息。

A TStatusBar component (located on the "Win32" page of the component palette) can be used to add a status bar to a form. A TStatusBar's Panels property is used to add, remove or modify the panels of the status bar (each panel is represented by a TStatusPanel object).

TStatusBar组件(位于组件面板的“ Win32”页面上)可用于向表单添加状态栏。 TStatusBar的Panels属性用于添加,删除或修改状态栏的面板(每个面板由TStatusPanel对象表示)。

A TProgressBar (located on the "Win32" page of the component palette) displays a simple progress bar. Progress bars provide users with visual feedback about the progress of a procedure within an application.

TProgressBar(位于组件面板的“ Win32”页面上)显示一个简单的进度栏。 进度栏为用户提供有关应用程序中过程进度的视觉反馈。

StatusBar中的ProgressBar ( ProgressBar in StatusBar )

When placed on a form the TStatusBar automatically aligns itself to the bottom (Align property = alBottom). Initially, it has just one panel.

当放置在窗体上时,TStatusBar会自动将其自身与底部对齐 ( Align属性= alBottom )。 最初,它只有一个面板。

Here's how to add panels to the Panels collection (once a status bar has been added to a form, let's say it has the default "StatusBar1" name):

这是将面板添加到Panels集合的方法(一旦状态栏已添加到表单,假设它具有默认的“ StatusBar1”名称):

  1. Double click the status bar component to open the Panels editor

    双击状态栏组件以打开面板编辑器

  2. Right click on the panel editor and select "Add." This adds one TStatusPanel object to the Panels collection. Add one more.

    右键单击面板编辑器,然后选择“添加”。 这会将一个TStatusPanel对象添加到Panels集合中。 再添加一个。
  3. Select the first Panel, and using the Object Inspector, assign "Progress:" for the ​Text property.

    选择第一个面板,使用对象检查,分配“进展情况:” Text属性。

  4. Note: we are to place a progress bar into the second panel!

    注意:我们要在第二个面板中放置一个进度条!
  5. Close the Panels editor

    关闭面板编辑器

To display a progress bar inside one of the Progress bar Panels, we first need a TProgressBar. Drop one on the form, leave the default name (ProgressBar1).

要在一个进度条面板中显示一个进度条,我们首先需要一个TProgressBar。 在窗体上放置一个,保留默认名称(ProgressBar1)。

Here's what needs to be done for ProgressBar to be displayed inside a StatusBar:

要使ProgressBar显示在StatusBar中,需要执行以下操作:

  1. Assign StatusBar1 for the Parent property of the ProgressBar1.

    为ProgressBar1的Parent属性分配StatusBar1。

  2. Change the Style property of the second StatusBar's panel to "psOwnerDraw." When set to psOwnerDraw, the content displayed in the status panel is drawn at runtime on the status bar’s canvas by code in an OnDrawPanel event handler. Opposite to "psOwnerDraw", the default value of "psText", ensures the string contained in the Text property is displayed in the status panel, using the alignment specified by Alignment property.

    将第二个StatusBar面板的Style属性更改为“ psOwnerDraw”。 当设置为psOwnerDraw时,状态面板上显示的内容将在运行时通过OnDrawPanel事件处理程序中的代码在状态栏的画布上绘制。 与默认值“ psText”的“ psOwnerDraw”相反,确保使用Alignment属性指定的对齐方式在状态面板中显示Text属性中包含的字符串。

  3. Handle the OnDrawPanel event of the StatusBar by adding the code that aligns the progress bar into a Panel of a status bar.

    通过添加将进度条与状态栏的面板对齐的代码来处理StatusBar的OnDrawPanel事件。

Here's the full code:

这是完整的代码:

The first two steps in the above discussion are done in the Form's OnCreate event handler.

上面讨论的前两个步骤是在Form的OnCreate事件处理程序中完成的。


procedure TForm1.FormCreate(Sender: TObject);
var
ProgressBarStyle: integer;
begin
//enable status bar 2nd Panel custom drawing
StatusBar1.Panels[1].Style := psOwnerDraw;
//place the progress bar into the status bar
ProgressBar1.Parent := StatusBar1;
//remove progress bar border
ProgressBarStyle := GetWindowLong(ProgressBar1.Handle,
GWL_EXSTYLE);
ProgressBarStyle := ProgressBarStyle
- WS_EX_STATICEDGE;
SetWindowLong(ProgressBar1.Handle,
GWL_EXSTYLE,
ProgressBarStyle);
end;

Note: the TProgressBar control has a default border that would look "ugly" when the component is placed in the status bar, so we decide to remove the border.

注意:TProgressBar控件具有默认边框,当将组件放置在状态栏中时,该边框看起来“丑陋”,因此我们决定删除该边框。

Finally, handle the OnDrawPanel event of the StatusBar1:

最后,处理StatusBar1的OnDrawPanel事件:


procedure TForm1.StatusBar1DrawPanel(
StatusBar: TStatusBar;
Panel: TStatusPanel;
const Rect: TRect);
begin
if Panel = StatusBar.Panels[1] then
with ProgressBar1 do begin
Top := Rect.Top;
Left := Rect.Left;
Width := Rect.Right - Rect.Left - 15;
Height := Rect.Bottom - Rect.Top;
end;
end;

All set. Run the project ... with some dummy code in the OnClick event handler of a Button:

可以了,好了。 在Button的OnClick事件处理程序中使用一些伪代码运行项目...:


procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
ProgressBar1.Position := 0;
ProgressBar1.Max := 100;
for i := 0 to 100 do
begin
ProgressBar1.Position := i;
Sleep(25);
//Application.ProcessMessages;
end;
end;

翻译自: https://www.thoughtco.com/placing-a-tprogressbar-into-a-tstatusbar-4092539

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值