在 C# 中闪烁文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/737195/
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-04 23:12:59  来源:igfitidea点击:

Blink text in C#

c#

提问by nikky

How do i blink the text in console using C#?

如何使用 C# 在控制台中闪烁文本?

采纳答案by Matt Brindley

Person-b was on the right track, but their code needs some changes:

Person-b 走在正确的轨道上,但他们的代码需要一些更改:

    static void Main()
    {
        string txt = "Hello, world!";
        while (true)
        {
            WriteBlinkingText(txt, 500, true);
            WriteBlinkingText(txt, 500, false);
        }
    }

    private static void WriteBlinkingText(string text, int delay, bool visible)
    {
        if (visible)
            Console.Write(text);
        else
            for (int i = 0; i < text.Length; i++)
                Console.Write(" ");
        Console.CursorLeft -= text.Length;
        System.Threading.Thread.Sleep(delay);
    }

EDIT: Refactored the code a little

编辑:稍微重构代码

回答by nikky

string txt = "Hello, world!";
while ( doingSomething )
{
  Console.Write(txt);
  System.Threading.Thread.Sleep(20);
  Console.CursorLeft -= txt.Length;
  for ( int i = 0; i < txt.Length; i++ )
    Console.Write(" ");
}

this code does not make it blink though

这段代码并没有让它闪烁

回答by Richard

There is no direct support, you will need to overwrite the text with spaces (or in different colours) based on a timer.

没有直接支持,您需要根据计时器用空格(或不同颜色)覆盖文本。

回答by Sune Rievers

I improved Matthew's code a bit using \rand some fancy use of String:

我改进了 Matthew 的代码,\r并使用了一些奇特的用法String

static void Main()
{
    string txt = "Hello, world!";
    WriteBlinkingText(txt, 500);
}

private static void WriteBlinkingText(string text, int delay)
{
    bool visible = true;
    while (true)
    {
        Console.Write("\r" + (visible ? text : new String(' ', text.Length)));
        System.Threading.Thread.Sleep(delay);
        visible = !visible;
    }
}

I also think that the WriteBlinkingTextmethod should be self-contained, so the loop is inside here, but that's just a matter of personal taste I guess :)

我也认为该WriteBlinkingText方法应该是独立的,所以循环就在这里,但这只是我个人品味的问题:)

回答by Cain532

    private void timer1_Tick(object sender, EventArgs e)//This might work better for you :)
    {
        Random rand = new Random();
        for (int i = 0; i < 255; i++)
        {
            int A = rand.Next(i);
            int R = rand.Next(i);
            int G = rand.Next(i);
            int B = rand.Next(i);
            label2.ForeColor = Color.FromArgb(A, R, G, B);
        }
    }

回答by hansthedude

To do this you will have to use:

为此,您必须使用:

console.clear

控制台清除

which will clear all the information in the console, but allow you to simulate a blinking text, by doing the following code you can do this: (This is in VB but its easily translated)

这将清除控制台中的所有信息,但允许您模拟闪烁的文本,通过执行以下代码,您可以执行此操作:(这是在 VB 中,但很容易翻译)

Dim Flash As Integer
Flash = 0
While Flash < 100 (Any number can be put here for duration)
Flash = Flash + 1
console.Backgroundcolor = consolecolor.Black
console.clear
system.threading.thread.sleep(25)
console.Backgroundcolor = consolecolor.White
console.clear
system.threading.thread.sleep(25)
End While

And that would give a blinking screen for example, for the blinking text simply adjust it:

例如,这将给出一个闪烁的屏幕,对于闪烁的文本,只需对其进行调整:

Dim FlashWord As Integer
FlashWord = 0
While FlashWord < 100 (Any number can be put here for duration)
FlashWord = FlashWord + 1
console.Foregroundcolor = consolecolor.Black
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
console.Foregroundcolor = consolecolor.White
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
End While

And this will simulate 'flashing', the only down side is that you lose your previous on-screen info, but nothing else, and for an effect this is pretty good!

这将模拟“闪烁”,唯一的缺点是您会丢失以前的屏幕信息,但没有其他内容,而且效果非常好!

回答by It Wasn't Me



enter image description here

在此处输入图片说明

Why not use backspace and rewrite the text on the same screen line?

为什么不使用退格键并在同一屏幕行上重写文本?

string blink = "Password Please!"

while (!System.Console.KeyAvailable)
   {
      Console.Write(blink);
      Thread.Sleep(650);

      for (int j = 1; j <= blink.Length + 2; j++)
          {
             Console.Write("\b"+(char)32+"\b");
             if (j == blink.Length + 2) Thread.Sleep(650);
          }
   }