10

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);
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
BRHSM
  • 854
  • 3
  • 13
  • 48
  • I think you need `conio.h` on windows/dos, instead of `system("cls")` and such, pehaps you should be able to control the cursro too. On *nix os's there are some control charaters to do that, I don't know what the equivalent should be in windows. – Iharob Al Asimi May 08 '15 at 14:40
  • Possible duplicate of [Hide cursor on remote terminal](http://stackoverflow.com/questions/2649733/hide-cursor-on-remote-terminal)? – Aaron Critchley May 08 '15 at 14:43
  • @iharob , I don't know what `conio.h` does, nor do I know what the correct statments are when using it. – BRHSM May 08 '15 at 14:43
  • @AaronCritchley I saw that question, but I didn't know what a remote terminal was, even after reading the linked question in that question – BRHSM May 08 '15 at 14:44

4 Answers4

28

To extend on Bishal's answer:

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

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

Source

hamish sams
  • 433
  • 4
  • 9
  • An important note to make to those using this solution, `\e` is non-ISO-standard escape sequence. You might want to use `\33` instead which does the same thing but is ISO-standard. – Mahyar Mirrashed Aug 11 '22 at 21:55
10

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

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • There is a small blink of the cursor when launching but that gives a cool effect. kinda like you are launching a big program that has a bit of trouble on startup haha. – BRHSM May 08 '15 at 14:54
1
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.

slfan
  • 8,950
  • 115
  • 65
  • 78
0

Here a solution that works both in Windows and Linux:

#include <iostream>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#endif // _WIN32
using namespace std;

void show_console_cursor(const bool show) {
#if defined(_WIN32)
    static const HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(handle, &cci);
    cci.bVisible = show; // show/hide cursor
    SetConsoleCursorInfo(handle, &cci);
#elif defined(__linux__)
    cout << (show ? "\033[?25h" : "\033[?25l"); // show/hide cursor
#endif // Windows/Linux
}
ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34