开发者

How I manage TProgressBar in Delphi with process completed on TImage?

I have one progressbar and an image on my form. Now I am doing some processing on each scanline on image. I wanted to show progress of my process using progressbar. I set the following properties of progressbar :

Max = 20
Min = 0
Position = 0
Step = 1
Smooth = False

Now my code on buttonclick event is as under :

   stat := imgmain.Picture.Bitmap.Height div pbImage.Max;
   Curstatus := 0;
   for cnt := 0 to img.Picture.Bitmap.Height - 1 do begin
      for cnt1 := 0 to img.Picture.Bitmap.Width - 1 do begin
        //Some processing
      end;
      if cnt > (Curstatus + stat) then begin
         pgimg.StepIt;
         Curstatus := cnt + stat;
      end;    
   end;

My problem is when I run this code, the if block is executed exactly 20 times but it shows more than开发者_如何学C 20 steps on my progress bar. Also the last step is shown incomplete. Note that smooth property is set to false.


With

Max = 20 
Min = 0 

You actually have 21 steps in your progress bar, with 0 being the first and 20 being the last. Set Max to 19, and it should work.


Min, Max and Position are used so the Progressbar can automatically how much work has been completed. Example:

Min := 100;
Max := 150;
Position := 125;
=> 50% work complete, the progress bar will be half-filled with green blocks.

Step is used so the progress bar knows how much to add to "Position" when you call StepIt. For example, if you'd do a progress bar for a file-copy operation, you might use something like this:

Min := 0;
Max := 2 * 1024 * 1024; // 2Mb
Step := 4 * 1024; // 4Kb
Position := 0;

// in a loop, copy 4Kb blocks, and then do:
ProgressBar.StepIt; // this is equivalent to Position := Position + Step;

Min and Max have nothing to do with the number of blocks you see on screen, that's entirely implementation-specific. For example Windows versions before XP didn't show blocks, they showed a simple filled bar. The size of the blocks might even be theme-dependent! Windows will show sufficient blocks to express the percentage of complete work. Min, Max and Position are used to calculate that percentage.


Set the Max value to the height of your image and increase position after processing (or before) each scan line.

The only progress bar I know of that will behave the way you want is the "TJvXPProgressBar" (part of JEDI VCL here) which will increase the number of blocks dsiplayed when Position is increased so with Max := 20, Min := 1 and Position := 5 there will be 5 blocks displayed.


Just a tip:

If you change position value prior to do the job inside the loop, you will get a better visual effect, more if after last loop there is not much work left after the loop till progress is hide.

In other words:

TheProgressBar.Min:=0;
TheProgressBar.Max:=20;
TheProgressBar.Position:=0;
for i:=1 to 20
do begin
        // Do some job that take some time
        TheProgressBar.Position:=TheProgressBar.Position+1;
   end;
TheProgressBar.Visible:=False; // Or window is closed, etc

That will never show progressbar to rich the end, it will rich the end, but as soon as shown, the progressbar is hidden or window closed, etc, so user has no time to see it ends.

Just do the increment before the job, progressbar will show not how much has been done yet, but will show in what step it is working, so last step will be shown for as long as last step tooks, so user will se that progressbar be filled for a while.

And allways remember a progressbar has Max-Min+1 possible positions... so if Min is zero it will have one more than max value: first possition will be 0, second will be 1, etc, last one will be max.

If you want from 1 to 20, set min to 1, not to zero.

Theese are jsut tips, in case someone do not think on them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜