Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide console cursor in c?

I have a simple C program that represents a loading screen within the console, but I can't get the cursor to hide. I tried cranking up the speed of the sleep function so that the cursor timer would be reset and the cursor would be gone but that doesn't work.

Any tips on how to hide the cursor?

Code:

#include <stdio.h>
#include <stdlib.h>

const int TIME = 1;

int main(int argc,char *argv[]){
    int i;
    while (1){
        printf("loading");
        for (i=0;i<3;i++){
            sleep(TIME);
            printf(".");
        }
        sleep(TIME);
        printf("\r");
        system("Cls");
        sleep(TIME);
    }
}
like image 282
BRHSM Avatar asked May 08 '15 14:05

BRHSM


3 Answers

To extend on Bishal's answer:

To hide the cursor: printf("\e[?25l");

To re-enable the cursor: printf("\e[?25h");

Source

like image 55
hamish sams Avatar answered Oct 12 '22 12:10

hamish sams


Add to your program the following function

#include <windows.h>

void hidecursor()
{
   HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_CURSOR_INFO info;
   info.dwSize = 100;
   info.bVisible = FALSE;
   SetConsoleCursorInfo(consoleHandle, &info);
}

and call it in your main.

And read more in the MSDN

like image 20
VolAnd Avatar answered Oct 12 '22 12:10

VolAnd


printf("\e[?25l");

This should work ! It is taken from ANSI codesheet where the characters are not just what they are seen. They act like some form of commands.

like image 45
Bishal Bashyal Avatar answered Oct 12 '22 14:10

Bishal Bashyal