如何在用 C++ 编写的控制台应用程序中制作加载动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8486181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:39:44  来源:igfitidea点击:

How to make a loading animation in Console Application written in C++?

c++animationconsolereplaceloading

提问by Aristona

I am coding a Console Application in c++ and I need to make something like a "loading.gif" just by using ASCII characters.

我正在用 C++ 编写一个控制台应用程序,我需要使用 ASCII 字符来制作类似“loading.gif”的东西。

The following is the list of the characters that I should be using:

以下是我应该使用的字符列表:

  1. --
  2. \
  3. |
  4. /
  5. --
  1. ——
  2. \
  3. |
  4. /
  5. ——

These symbols will make a loading animation by cycling.

这些符号将通过循环制作加载动画。

However, when I write the output, it become like:

但是,当我编写输出时,它变得像:

Output line 1:-- Output line 2:\ Output line 3:| Output line 4:/ Output line 5:--

输出行 1:-- 输出行 2:\ 输出行 3:| 输出线 4:/ 输出线 5:-

I need to do this like this:

我需要这样做:

Output line 1:[this will be replaced all the time]

输出第 1 行:[这将一直被替换]

It should never go to the second line.

它永远不应该进入第二行。

How can I do this in C++? Is there any kind of replacefunction?

我怎样才能在 C++ 中做到这一点?有什么替换功能吗?

回答by Mike Seymour

You can use the backspace character ('\b') to go back and overwrite characters on the console. You'll also need to flush the output after each change, otherwise the output might stay in a buffer and not appear on the console.

您可以使用退格字符 ( '\b') 返回并覆盖控制台上的字符。您还需要在每次更改后刷新输出,否则输出可能会保留在缓冲区中而不会出现在控制台上。

Here is a simple example:

这是一个简单的例子:

#include <iostream>
#include <unistd.h> // for sleep()

int main()
{
    std::cout << '-' << std::flush;
    for (;;) {
        sleep(1);
        std::cout << "\b\" << std::flush;
        sleep(1);
        std::cout << "\b|" << std::flush;
        sleep(1);
        std::cout << "\b/" << std::flush;
        sleep(1);
        std::cout << "\b-" << std::flush;
    }
}

回答by Software_Designer

The Microsoft Windows version.

微软视窗版本。

#include <iostream>
#include <windows.h> // for sleep()

int main()
{
    std::cout << '-' << std::flush;
    for (;;) {
        Sleep(10);
        std::cout << "\b\" << std::flush;
        Sleep(10);
        std::cout << "\b|" << std::flush;
        Sleep(10);
        std::cout << "\b/" << std::flush;
        Sleep(10);
        std::cout << "\b-" << std::flush;
    }

    return 0;
}

回答by Taimoor Haider

Mike Seymour's Logic used but with a realistic look.

使用了 Mike Seymour 的 Logic,但外观逼真。

#include <iostream>
#include <unistd.h> // for sleep()

int main()
{

    std::cout << "Loading Please Wait";
    while (true)
    {
        sleep(1);
        std::cout << "." << std::flush;
        sleep(1);
        std::cout << "." << std::flush;
        sleep(1);
        std::cout << "." << std::flush;
        sleep(1);
        std::cout << "\b\b\b   \b\b\b" << std::flush;
    }
}

回答by codeling

Staying very simple, you can use the backspace character, as explained here, which actually appears to be a duplicate of your question. For more complicated things, it gets platform specific; to set the cursor position maybe this can help, if you're on Windows.

保持非常简单,您可以使用退格字符,如解释here,它实际上似乎与您的问题重复。对于更复杂的事情,它是特定于平台的;设置光标位置也许这会有所帮助,如果您使用的是 Windows。

回答by C0LD

#include <iostream>
#include <stdio.h>
#include <windows.h>

int main()
{
    for (;;) {

        std::cout << "\b\b\b\b\b\b\b\b\b\bLoading   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLOading   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoAding   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoaDing   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoadIng   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoadiNg   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoadinG   " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoading.  " << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoading.." << std::flush;
        Sleep(100);
        std::cout << "\b\b\b\b\b\b\b\b\b\bLoading..." << std::flush;
        Sleep(100);
    }
}

回答by scx

And a cross-platform answer:

和一个跨平台的答案:

#include <iostream>
#include <chrono>
#include <thread>

/* Comment for c++11 solution. */
using namespace std::literals::chrono_literals;

int main(int argc, char** argv)
{
    /* Uncomment for c++11 solution. */
    // std::chrono::seconds s(1);

    std::cout << '-' << std::flush;
    for (;;) {
        std::this_thread::sleep_for(1s);
        std::cout << "\b\" << std::flush;
        std::this_thread::sleep_for(1s);
        std::cout << "\b|" << std::flush;
        std::this_thread::sleep_for(1s);
        std::cout << "\b/" << std::flush;
        std::this_thread::sleep_for(1s);
        std::cout << "\b-" << std::flush;
    }

    return 0;
}

Note that chrono_literals are only available in c++14, you can use std::chrono::seconds s(1);instead, passing sto std::thread::sleep_for();

请注意,chrono_literals 仅在 c++14 中可用,您可以std::chrono::seconds s(1);改为使用,传递sstd::thread::sleep_for();

回答by l3mpik

for(i=0; i<100; i++)
{
    system("cls");
    cout << "Loading: " << i << " %  \n";
    if(i==100)
    {    
        redirect to void or... ?
    }
}