Delphi Displays the process bar in the status bar

Source: Internet
Author: User

Friends who often surf the internet may have already found that at the bottom of some browsers in some cases dynamically display a process bar, for example, when the page is displayed, if the time is longer, there will be a grid forward, to remind users how much data has been sent down. Not only is it in the browser, it is often used to download software, such as the most popular BT download software, which is now used in this technology. The improvement of technology stems from the needs of the work, now writing network software so popular, the above tips should be every one like us programming enthusiasts need it.

Open Delphi, select the status bar (StatusBar) component on the WIN32 panel and place it on the form (Figure 1), by default the status bar component will automatically be placed at the bottom of the form (that is, the Align property is set to Albottom), At the same time the status bar is initially a panel, in order to make the following statement clear, let's add a panel to the status bar, the following steps:

1. Double-click the status bar component to open the Panel editor (Panels editor).

2. Right-click the Panel editor and select "Add" to add a panel.

3. Select the first panel, select the Text property in the object Inspector, and write "Progress:".

4. Close the Panel editor.

Just have a status bar of course not, today's protagonist should be progress (process bar) is right, now we put the process bar on the form. (Figure 2)

After seeing the picture above, some impatient readers may ask, how is the process on top of the state bar, not inside it, or on the panel? This is not the result I want! Don't worry, don't worry, the following is today's finale, put the process bar on the status bar panel, but also to make it move. Action steps are as follows:

1. Set the ProgressBar Parent property to StatusBar.

2. Changing the Style property of the second panel of StatusBar to "Psownerdraw" is the key to solving the problem, and when the style is set to Psownerdraw, the panel can be placed on other components, The way to do this is to write code in the Ondrawpanel event, which is set to Pstext, so that only text can be displayed, just like the first panel.

The steps above are implemented when you actually write code:

1.//First write code in the Formcreate event
Procedure tform1.formcreate (sender:tobject);
var
Progressbarstyle:integer;
Begin
//Set the second panel of the status bar to a custom (i.e. Psownerdraw)
Statusbar1.panels[1]. Style: = Psownerdraw; 
//Put the process bar into the status bar
Progressbar1.parent: = StatusBar1;
  Removes the border of the status bar, which dissolves into the status bar
Progressbarstyle: = GetWindowLong (Progressbar1.handle,gwl_exstyle);
Progressbarstyle: = Progressbarstyle-ws_ex_staticedge;
SetWindowLong (Progressbar1.handle, Gwl_exstyle, Progressbarstyle);
End;
2.//writes the custom code for the status bar
procedure Tform1.statusbar1drawpanel (Statusbar:tstatusbar; Panel:tstatuspanel;const rect:trect);
Begin
//Note that the panels[1] refers to the 2nd panel, because the
if Panel = statusbar.panels[1] Then
with the ProgressBar1 do is the default starting from 0.    Gin
Top: = rect.top;
Left: = Rect.left;
Width: = rect.right-rect.left-15;
Height: = Rect.bottom-rect.top;
End;
End;

After the key problem is solved, let's take a small example, so that we can have a global impression, the control is placed as shown in (Figure 3), write the following code:

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);
end;
end;

Run this applet, click on the button, see, the process bar in the status bar to move up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.