Blink text in C#

前端 未结 7 1247
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 03:17

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

相关标签:
7条回答
  • 2021-01-14 03:23
    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

    0 讨论(0)
  • 2021-01-14 03:31

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

    0 讨论(0)
  • 2021-01-14 03:34

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

    0 讨论(0)
  • 2021-01-14 03:43

    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)

    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!

    0 讨论(0)
  • 2021-01-14 03:45

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

        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

    0 讨论(0)
  • 2021-01-14 03:45

    I improved Matthew's code a bit using \r and some fancy use of 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 WriteBlinkingText method should be self-contained, so the loop is inside here, but that's just a matter of personal taste I guess :)

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