How to blink particular text continuously all time during input and output?

a 夏天 提交于 2019-12-23 03:14:41

问题


Here is my code which blinks 'Welcome' after user enter his name.

'Welcome' does not blink when user is writing his name. As user hits enter then caret goes into the while loop. Then caret position is set back to the coordinates of 'Welcome' & cout prints 'Welcome' with 5 colors again & again so it seems that 'Welcome' is blinking.

But I want that 'Welcome' blinks continuously as the program starts.

So more likely this question also ask - can we have two caret/cursor at the same time ?

#include <iostream>
#include <conio.h> 
#include <windows.h>
 using namespace std;
 int main(int argc, char** argv)
 {
   int x,y,i;char name[10];
   textcolor(10);
   x=wherex();y=wherey();       //corrdinates of caret will be stored in x & y.
   cout<<"\t\t\t\tWelcome\n";
   textcolor(15);
   cout<<"\nEnter your name\n";
   gets(name);
   while(1)
   {
      for(i=10;i<15;i++)
      {
         textcolor(i);
         gotoxy(x,y);          //Transferring caret to the coordinates stored in x & y.
         cout<<"\t\t\t\tWelcome";
         Sleep(300);
      }
   }
   return 0;
 }

回答1:


I wrote a small code for this question , it's not 100% correct answer. I am just posting this answer just for giving little bit idea to newbie.

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int x,y,b,l,n=0;
char c;
void blink()
{
    {
        int m;
        for(m=10;m<15;m++)
        {
            textcolor(m);
            gotoxy(x,y);
            cout<<"\t\t\t\tWelcome";
            Sleep(60);
        }
   }
}
int main(int argc, char** argv)
{
   char i;int key_stroke;
   textcolor(10);
   x=wherex();y=wherey();
   cout<<"\t\t\t\tWelcome\n";
   textcolor(15);
   cout<<"\nEnter your name\n";
   l=wherex();b=wherey();
   z:
   {
     while (1)
     {
          if(!(_kbhit()))
        {
               blink();
                 goto z;
        }
        else 
          {
             i=_getch();
             if(i==13)
             {
                gotoxy(l+n,b+1);
                    return 0;
               }
               textcolor(10);
               gotoxy(l+n,b);
               cout<<i;n=n+1;
          }
     }
   }
   return 0;
}



回答2:


No we cant have two caret/cursor at the same time. User inputs name first.

It begins to blink right after the user has pressed the enter key by first displaying the text in a given color and time delay. Then after it sets the color to black and overwrites the text wth black color.

Windows code:

#include <iostream>
#include <conio.h> 
#include <windows.h>
 using namespace std;

void gotoxy(int x, int y);
void setcolor(WORD color);
void clrscr(); 


 int main(int argc, char** argv){

   int x,y,i;char name[10];

   setcolor(10);
   cout<<"Welcome\n";

   setcolor(15);
   cout<<"\nEnter your name  ";
   gets(name);

   i=0;
   x=22;
   y=12;

   while(1) {

         // counter for text color
         i++; if (i>15) i=1;

         // print colored text
         setcolor(i);
         gotoxy(x,y);          
         cout<<"Welcome  "<<name;
         Sleep(100);


         // Print black text to simulate blink
         setcolor(0);
         gotoxy(x,y);           
         cout<<"                        ";
         Sleep(100);



   }

   setcolor(7);
   gotoxy(1,24);
   return 0;
 }



void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}



void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}




void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    return;
}



回答3:


Instead of such Codes you can use BLINK in textcolor() function. the only problem is that you cannot control its speed. thats it. otherwise its easy to use and you can also set text color. Eg.

textcolor ( RED + BLINK ) ; cprintf ( " /t/t WELCOME " ) ;

thats it. I didn't had time to read your full question and program. Also I am just a newbie. So I hope this helps you and others.



来源:https://stackoverflow.com/questions/31274000/how-to-blink-particular-text-continuously-all-time-during-input-and-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!