登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

Placing a TProgressBar into a TStatusBar  

2010-11-04 14:10:30|  分类: Delphi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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).

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.

Win32 Delphi component palette with TStatusBar and TProgresBar

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.
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):

  1. Double click the status bar component to open the Panels editor
  2. Right click on the panel editor and select "Add" - this ads one TStatusPanel object to the Panels collection. Add one more.
  3. Select the first Panel, and using the Object Inspector, assign "Progress:" for the Text property.
  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 Panel's, we first need a TProgressBar. Drop one on the form, leave the default name (ProgressBar1).

TProgressBar in TStatusBar at design time

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

  1. Assign StatusBar1 for the Parent property of the ProgressBar1. Hint: "Parent vs. Owner"
  2. Change the Style property of the second StatusBar's panel to "psOwnerDraw". Hint: "Owner drawing in Delphi" 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.
  3. Handle the OnDrawPanel event of the StatusBar by adding the code that aligns the progress bar into a Panel of a status bar.

Here's the full code:

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

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.

Finally, handle the OnDrawPanel event of the StatusBar1:

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:

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;

TProgressBar in TStatusBar at run time

Progress bar in a ListView?!
Here's how to add a progress bar to a ListView control. Plus: full source code to the TListViewEx component (TListView descendant) with ColumnResize events!

Progress bar in a message box?!
Let's say you have a standard Windows dialog box displaying a question to the user with "Yes" and "No" buttons. Wouldn't it be great if a progress bar could be displayed within a dialog box "counting" seconds until the dialog box automatically closes itself?
Here's how to place a progress bar inside a standard dialog box!

TAnyOtherControl in StatusBar?

Yes, you can add any control you like to a status bar ... just follow the steps you've done with the progres bar! If you need help, be sure to post your question to the Delphi Programming Forum.

  评论这张
 
阅读(932)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018