How to hide console cursor in c?
Asked Answered
D

4

11

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);
    }
}
Dubenko answered 8/5, 2015 at 14:38 Comment(4)
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.Leaky
Possible duplicate of Hide cursor on remote terminal?Nazareth
@iharob , I don't know what conio.h does, nor do I know what the correct statments are when using it.Dubenko
@AaronCritchley I saw that question, but I didn't know what a remote terminal was, even after reading the linked question in that questionDubenko
L
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

Lorraine answered 8/5, 2015 at 14:48 Comment(1)
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.Dubenko
B
30

To extend on Bishal's answer:

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

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

Source

Bawdyhouse answered 23/3, 2019 at 12:12 Comment(1)
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.Alburga
L
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

Lorraine answered 8/5, 2015 at 14:48 Comment(1)
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.Dubenko
D
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.

Deerhound answered 1/3, 2019 at 5:20 Comment(0)
A
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
}
Archenemy answered 25/5, 2021 at 18:32 Comment(1)
My good sir, this is C not C++.Myocardiograph

© 2022 - 2024 — McMap. All rights reserved.