灵活的控制光标在控制台移动

  1. 利用结构指针实现对光标位置的实时修改以及返回方便再次调用时光标还在原位
  2. 具体使用方法最后有写的......
  3. /** 移动光标并位置 
  4. * coord:光标初始坐标 
  5. * span:光标横向移动跨度 
  6. * column:光标最多移动列数 
  7. * high:光标可移动最高行数 
  8. * low:光标可移动最低行数 
  9. */  
  10. HANDLE winHandle;   //句柄  
  11. COORD * movePosition(COORD coord, int span, int column, int high, int low);  
  12. COORD * movePosition(COORD coord, int span, int column, int high, int low){  
  13.     winHandle = GetStdHandle(STD_OUTPUT_HANDLE);//拿到控制台句柄  
  14.     COORD * pos = &coord;                                //屏幕上的坐标  
  15.     SetPosition(pos->X, pos->Y);              //设置初始光标位置  
  16.     // CONSOLE_SCREEN_BUFFER_INFO csbi;            //控制台屏幕缓冲区信息  
  17.     int ch;                                     //输入字符串  
  18.     ch = getch();  
  19.     if(ch == 0xE0 || ch == 0x0d)                      //0x0d表示回车,0XE0表示上下左右等键的键码  
  20.     {  
  21.         //GetConsoleScreenBufferInfo(winHandle, &csbi);  
  22.         //pos->X = csbi.dwCursorPosition.X;       //得到坐标X的值  
  23.         //pos->Y = csbi.dwCursorPosition.Y;       //得到坐标Y的值  
  24.     
  25.         if(ch == '\n')                  //如果是回车  
  26.         {  
  27.             return NULL;  
  28.         }  
  29.         ch = getch();  
  30.         if(ch == 72)                    //  
  31.         {  
  32.             if(pos->Y > high) {  
  33.                 pos->Y--;  
  34.             }else{  
  35.                 pos->Y = low;  
  36.             }  
  37.             SetPosition(pos->X, pos->Y);  
  38.         }else if(ch == 80)              //  
  39.         {  
  40.             if(pos->Y < low) {  
  41.                 pos->Y++;  
  42.             }else{  
  43.                 pos->Y = high;  
  44.             }  
  45.             SetPosition(pos->X, pos->Y);  
  46.         }  
  47.     
  48.         else if(ch == 75)               //  
  49.         {  
  50.             if(pos->X > MARGIN_X + 3)  
  51.                 pos->X -= span;  
  52.             else {  
  53.                 pos->X = MARGIN_X + 3 + span * (column - 1);  
  54.                 if(pos->Y > high) {  
  55.                     pos->Y--;  
  56.                 }  
  57.             }  
  58.             SetPosition(pos->X, pos->Y);  
  59.         }  
  60.     
  61.         else if(ch == 77)               //  
  62.         {  
  63.             if(pos->X < MARGIN_X + 3 + span * (column - 1))  
  64.                 pos->X += span;  
  65.             else  
  66.             {  
  67.                 pos->X = MARGIN_X + 3;  
  68.                 if(pos->Y < low)  
  69.                     pos->Y++;  
  70.             }  
  71.             SetPosition(pos->X, pos->Y);  
  72.         }  
  73.         SetConsoleCursorPosition(winHandle, *pos);  
  74.     }  
  75.     return pos;  
  76. }  
  77. /* 设置光标的位置 */  
  78. void SetPosition(int x, int y);  
  79. void SetPosition(int x, int y)  
  80. {  
  81.     COORD pos = {x, y};  
  82.     winHandle = GetStdHandle(STD_OUTPUT_HANDLE); //拿到控制台句柄  
  83.     // 设置光标的坐标  
  84.     SetConsoleCursorPosition(winHandle, pos);  
  85.  
  86.  
  87. int main()  
  88. {  
  89.     COORD c = {13, 3};  
  90.     while(1){  
  91.         c = *movePosition(c,9,8,3,10);  
  92.         SetPosition(10,12);  
  93.         printf("x : %d, y: %d", c.X, c.Y);  
  94.         SetPosition(c.X, c.Y);    
  95.     }  
  96. }  

猜你喜欢

转载自www.cnblogs.com/11-shou/p/12380005.html