Advertisement
AyrA

Console cursor positioning for windows

Dec 26th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <conio.h>
  4.  
  5. int moveTo(int x, int y){
  6.     COORD coord;
  7.     coord.X = x;
  8.     coord.Y = y;
  9.     HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  10.     return SetConsoleCursorPosition(hConsole, coord);
  11. }
  12.  
  13. void getCursorPosition(int* x, int* y){
  14.     HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  15.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  16.     GetConsoleScreenBufferInfo(hConsole,&csbi);
  17.     *x=csbi.dwCursorPosition.X;
  18.     *y=csbi.dwCursorPosition.Y;
  19. }
  20.  
  21.  
  22. void moveToStart()
  23. {
  24.     int x;
  25.     int y;
  26.     //Get Position
  27.     getCursorPosition(&x,&y);
  28.     //Move to Start
  29.     moveTo(0,y);
  30. }
  31.  
  32. void fillLine(){
  33.     int width;
  34.     int i=0;
  35.     HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  36.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  37.     GetConsoleScreenBufferInfo(hConsole,&csbi);
  38.     width=csbi.dwSize.X;
  39.     moveToStart();
  40.     for(i=0;i<width;i++){
  41.         putchar(' ');
  42.     }
  43.     moveTo(0,csbi.dwCursorPosition.Y);
  44. }
  45.  
  46. int ask(const char* Frage){
  47.     int i=0;
  48.     printf("%s [y/n]:", Frage);
  49.     while(i!='y' && i!='n' && i!='Y' && i!='N'){
  50.         i=getch();
  51.     }
  52.     putchar('\n');
  53.     return i=='y' || i=='Y';
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement