how to display progress bar?

后端 未结 1 496
囚心锁ツ
囚心锁ツ 2021-01-15 22:49

I created a steganography(hide text in bitmap) application and I want to add a progress bar to show how long the process works.

procedure TForm1.Button2Click         


        
相关标签:
1条回答
  • 2021-01-15 23:25

    First, you need to move the code to its own thread. Otherwise the GUI will not respond. You also have to make sure the code is thread-safe.

    Anyhow, inside the thread, you need to update the progress bar every now and then. If it so happens that you have an outer loop and an inner loop, where the outer loop iterates once a second or so, you could update the progress bar at that place. If you only have one big loop, you might not want to update the progress bar at every iteration; for instance, a single iteration might perhaps be done in only a few milliseconds.

    Instead, you can make sure to update the progress bar once every second, or so. To accomplish this, you can use GetTickCount:

    tc := GetTickCount;
    if Terminated then Exit;
    if tc - oldtc > 1000 then
    begin
      PostMessage(FProgressBarHandle, PBM_SETPOS, TheNewPosition, 0);
      oldtc := tc;
    end;
    

    This also shows a way to update the progress bar -- simply post it a message! You could also define your own message and send it to the main from.

    0 讨论(0)
提交回复
热议问题