2017-11-10 448 views
2

我想在漫長的過程中在我的控制檯應用程序中顯示某種動畫,並且不知道如何執行此操作。如何使用Delphi在控制檯應用程序中顯示進度條?

我已經做了一項研究,但是我找到的解決方案沒有引起我的興趣,或者我不樂意瞭解它們。

我的應用程序加載一個文本文件,並通過搜索要替換的單詞來遍歷所有行。

它可能是一個進度條或任何循環動畫。

+2

如果您的進度指示器只是一個遞增的整數,請使用'write'(而不是'writeln')將它寫入控制檯,然後再回車。這樣,它會覆蓋以前的值。 – MartynA

+0

@Jerry Dodge該應用程序稍微複雜一點,只是解釋了你想改變的部分。 – Anderson

+0

我更喜歡字符「 - \ |」的順序/',在屏幕上的相同位置重複,直到過程完成。 –

回答

9

下面是一個示例,它將生成消息在循環中處理Y(Z%)...的X,延遲時間表示在循環中執行某些操作的時間。顯然,這是一個人爲的例子,但它表明了一種可能性。 (也很明顯,你將取代值的循環和Y具有有意義的值的消息中的上限值,例如TStringList.Count。)

program Project1; 

{$APPTYPE CONSOLE} 

uses 
    System.SysUtils; 

var 
    i: Integer; 
    StopValue, Pct: Integer; 

(* 
    #13 is a carriage return, which moves the cursor back to the 
    left side of the console without adding a line feed (#10). It 
    allows writing on the same line over the same content without 
    moving to the next line. See the demo output. 
*) 
const 
    StatusMsg = #13'Processing %d of %d (%d%%) ...'; 

begin 
    StopValue := 150;  // Replace with your upper limit, e.g. StringList.Count 
    for i := 1 to StopValue do 
    begin 
    Pct := Trunc((i * 1.0/StopValue) * 100); 
    Write(Format(StatusMsg, [i, StopValue, Pct])); 
    (**************************************************************** 
     Note: The call to Sleep here is only to introduce an artificial 
     delay in the loop in order to allow the progress to be seen. 
     Otherwise, the empty loop runs so fast that it's not clear when 
     the progress increments are shown. 

     Clearly, you would replace the call to Sleep with your code to 
     actually do some work, such as processing each line of the text 
     file. 

     Explained in detail for clarity, as some commenters have indicated 
     they're not capable of understanding why a call to Sleep is used 
     here, so adding this unnecessarily large comment is needed for them. 
    ****************************************************************) 
    Sleep(250); 
    end; 
    Write(#13'Processing complete. Press Enter to quit.'); 
    ReadLn; 
end. 

快照進度指示器的

enter image description here

3

很久很久以前;一個用於與顯示綠色字母的管的大型機通信,類型爲VT100。自那時以來發生了很大變化,除了使用命令行界面的程序實際上仍然與(虛擬)VT100的後端通信。嚴重依賴於ASCII代碼,這32個最低值對於控制顯示在屏幕上的哪個位置很重要。這就是爲什麼一些新線路在某些環境下仍然編碼爲#13,這將在遠程型作家上做'回車',而#10將爲鏈式饋送紙張提供一條線路。通過修改位置來顯示下一個接收到的字符,VT100模擬了相同的情況。

所以到覆蓋什麼是在控制檯窗口中的屏幕上,只需發送#13和新的數據。例如:

procedure ShowProgress(Position,Max:integer); 
var 
    i,j:integer; 
const 
    BarWidth=28; //must be less than 79 
begin 
    Write(#13'['); 
    j:=Position*BarWidth div Max; 
    for i:=1 to BarWidth do 
    if i<=j then Write('#') else Write('-'); 
    Write(']'); 
end; 

,並用WriteLn;結束(這實際上是Write(#13#10);)或一些與#13和足夠長的時間來覆蓋整個酒吧其他前綴。

2

試試這個控制檯應用程序:

program Project1; 

{$APPTYPE CONSOLE} 

{$R *.res} 

uses 
    System.SysUtils, Windows; 


procedure ClearScreen; 
var 
    stdout: THandle; 
    csbi: TConsoleScreenBufferInfo; 
    ConsoleSize: DWORD; 
    NumWritten: DWORD; 
    Origin: TCoord; 
begin 
    stdout := GetStdHandle(STD_OUTPUT_HANDLE); 
    Win32Check(stdout<>INVALID_HANDLE_VALUE); 
    Win32Check(GetConsoleScreenBufferInfo(stdout, csbi)); 
    ConsoleSize := csbi.dwSize.X * csbi.dwSize.Y; 
    Origin.X := 0; 
    Origin.Y := 0; 
    Win32Check(FillConsoleOutputCharacter(stdout, ' ', ConsoleSize, Origin, 
    NumWritten)); 
    Win32Check(FillConsoleOutputAttribute(stdout, csbi.wAttributes, ConsoleSize, Origin, 
    NumWritten)); 
    Win32Check(SetConsoleCursorPosition(stdout, Origin)); 
end; 

var 
    iFileLineCount : Integer; 
    iCounter1,iCounter2 : Integer; 
    iPercent : Integer; 
begin 


    try 
    iFileLineCount := 12000; 
    for iCounter1 := 1 to iFileLineCount do 
     begin 

     //do your application thing like reading file 
     ClearScreen; 
     iPercent := iCounter1 * 100 div iFileLineCount; 
     for iCounter2 := 1 to iPercent do 
      write('|'); 
     write(IntToStr(iPercent) + '%'); 
     end; 
    except 
    on E: Exception do 
     Writeln(E.ClassName, ': ', E.Message); 
    end; 



end. 

這是舊的方式,以示對OS在控制檯應用程序的進度條做的一樣當然,有很多更好的方法,但它只是工作。 德里不支持CRT工具,所以我添加了一個Procedure清屏。

相關問題