Home > Net >  When I click the console, the cursor shows up again. How can I prevent that?
When I click the console, the cursor shows up again. How can I prevent that?

Time:05-19

I'm still new to this. so I'm basically on a visual studio project. something called a console application.

#include <windows.h>
#include <iostream>

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coords;
void setcursor(bool visible, DWORD size) { //if we set it to (0,0) in main. the cursor won't be there anymore. the size becomes 20, and it no longer becomes visible? that's the gist of it.
    CONSOLE_CURSOR_INFO lpCursor;
    if (size == 0) size = 20; //makes the cursor bigger so it won't show?
    lpCursor.bVisible = visible;
    lpCursor.dwSize = size;
    SetConsoleCursorInfo(console, &lpCursor);
}
void gotoxy(int x, int y) { //to change the coordinates the text outputs to on the cmd
    coords.X = x;
    coords.Y = y;
    SetConsoleCursorPosition(console, coords);
}

this thing(method?), below, called the "setcursor". after i make the cmd screen thing bigger and then even after I minimize it again afterwards. it stops working. the cursor thing appears again as though i never had that method set up. how do i make the cursor go for good.

int main()
{
    gotoxy(4, 4);
    setcursor(0, 0); //makes that cursor on the console that awaits your text input go invisible or something.
    int x;
    std::cin >> x;
    std::cout << x;
}

CodePudding user response:

The cursor in the command window will redisplay when you resize or minimize/restore the command window. You would need to recall this function setcursor(...) on every window shape change to make it invisible again.

The SetConsolCursorInfo function will reject a cursor size of 0. This is why you test for that and, prevent it in this line if (size == 0) size = 20;

CodePudding user response:

Based on JimmyNJ's answer. I figured that I need a way to detect window size changes, cause that void thinge resets back to 0 by itself. so i went ahead searched up questions on stack overflow on to that endeavor. link

I tried something with the answer given in the question above. It detects changes in the window buffer size (I have no idea what this means), perhaps the screen resolution?

Declare this above main()

HWND g_hWindow = GetConsoleWindow();
void CALLBACK EventProc(HWINEVENTHOOK hook, DWORD event, HWND wnd, LONG object, LONG child, DWORD thread, DWORD time) {
    setcursor(0, 0);
}

and inside main() write this:

HWINEVENTHOOK eventHook = SetWinEventHook(EVENT_CONSOLE_LAYOUT, EVENT_CONSOLE_LAYOUT, NULL, EventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
if (eventHook != 0) {
            setcursor(0, 0);
        }

Note that in the console game i am making, i have that 'if' in a do-while loop. Also this doesn't fix things properly. As again that void thinge likes to reset itself to zero. but when it does, this fixes it.

  •  Tags:  
  • c
  • Related