在C#中闪烁文本

时间:2009-04-10 09:53:32

标签: c#

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

7 个答案:

答案 0 :(得分:5)

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);
    }

编辑:重新编写代码

答案 1 :(得分:1)

为此,您必须使用:

console.clear

将清除控制台中的所有信息,但允许您模拟闪烁的文本,通过执行以下代码,您可以执行此操作: (这是在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

例如,这会给出一个闪烁的屏幕,因为闪烁的文字只需调整它:

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

这将模拟“闪烁”,唯一的缺点就是你丢失了之前的屏幕信息,但没有其他任何东西,而且效果非常好!

答案 2 :(得分:0)

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(" ");
}

这段代码不会让它眨眼

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我使用\r稍微改进了Matthew的代码,并使用了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;
    }
}

我还认为WriteBlinkingText方法应该是自包含的,所以循环就在这里,但这只是个人品味的问题我想:)

答案 5 :(得分:0)

    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);
        }
    }

答案 6 :(得分:0)


enter image description here

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

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);
          }
   }