1

ユーザーが自分の名前を入力した後に「ようこそ」を点滅させる私のコードは次のとおりです。

ユーザーが自分の名前を書いているときに「ようこそ」が点滅しません。ユーザーがEnterキーを押すと、キャレットがwhileループに入ります。すると、キャレットの位置が「ようこそ」の座標に戻り、cout が「ようこそ」を 5 色で繰り返し印刷するので、「ようこそ」が点滅しているように見えます。

しかし、プログラムの開始時に「ようこそ」が継続的に点滅することを望みます。

したがって、この質問も尋ねる可能性が高くなります-同時に2つのキャレット/カーソルを使用できますか?

#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;
 }
4

3 に答える 3

2

いいえ、同時に 2 つのキャレット/カーソルを持つことはできません。ユーザーは最初に名前を入力します。

ユーザーがエンターキーを押した直後に、最初にテキストを特定の色と時間遅延で表示することにより、点滅を開始します。次に、色を黒に設定し、テキストを黒で上書きします。

Windows コード:

#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;
}
于 2015-07-07T20:24:38.337 に答える