Link to home
Start Free TrialLog in
Avatar of Booth882
Booth882

asked on

console cursor shape

How do I change the shape of the console cursor?  Mainly I just want to turn it off, make it blank.  
Avatar of nietod
nietod

In DOS or in a win32 console app?
In DOS use int 10H Sub-function 1

Set AH to 1
CH to the cursor start line 0-15
CL to cursor end line 0-15.  
and execute interupt 10H.

To make cursor dissapear set the start and end lines 0.  To make the a big block cursor set start to 0 and end to 15.
Is this what you need?
Win32: SetConsoleCursorInfo()
Set the bVisible member of the CONSOLE_CURSOR_INFO structure to FALSE.

Docs say:

The SetConsoleCursorInfo function sets the size and visibility of the cursor for the specified console screen buffer.

BOOL SetConsoleCursorInfo(
    HANDLE hConsoleOutput,      // handle of console screen buffer  
    CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo       // address of cursor information
   );      
 
hConsoleOutput - Identifies a console screen buffer. The handle must have GENERIC_WRITE access.

lpConsoleCursorInfo - Points to a CONSOLE_CURSOR_INFO structure containing the new specifications for the screen buffer's cursor.
 
If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError.

When a screen buffer's cursor is visible, its appearance can vary, ranging from completely filling a character cell to showing up as a horizontal line at the bottom of the cell. The dwSize member of the CONSOLE_CURSOR_INFO structure specifies the percentage of a character cell that is filled by the cursor. If this member is less than 1 or greater than 100, SetConsoleCursorInfo fails.

The CONSOLE_CURSOR_INFO structure contains information about the console cursor.
typedef struct _CONSOLE_CURSOR_INFO { // cci  
    DWORD  dwSize;
    BOOL   bVisible;
} CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;
 
dwSize - Specifies a number between 1 and 100, indicating the percentage of the character cell that is filled by the cursor. The cursor appearance varies, ranging from completely filling the cell to showing up as a horizontal line at the bottom of the cell.

bVisible - Specifies the visibility of the cursor. If the cursor is visible, this member is TRUE.

Is that it?
Probably not, Alex.  If I remember correctly, from a different question he is programming in DOS.  But now we got both bases covered.
I dunno.  He may have switched over to the dark side.
Avatar of Booth882

ASKER

the dark side?
I tried the SetConsoleCursorInfo, heres the code

HANDLE hand;

hand = CreateConsoleScreenBuffer(GENERIC_READ |      GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_             BUFFER, NULL);
if(hand == INVALID_HANDLE_VALUE)
     //exception output, exit(1);

if(!SetConsoleCurrentScreenBuffer(hand))
     //exception output, exit(1);

CONSOLE_CURSOR_INFO * cursor;
cursor->bVisible = FALSE;

if(!SetConsoleCursorInfo(hand, cursor))
     //exception output, exit(1);

it compiles, but when I run it it says it performed an illegal operation and will be shut down.  When I go to the debugger it says

"unhandled exception in file.exe:0xC0000005(or something):Access Violation"

and points at the line

cursor->bVisible = FALSE;

Can you tell me what this means?
answer coming.
the dark side?
I used the SetConsoleCursorInfo.  heres my code:

HANDLE hand;
CONSOLE_CURSOR_INFO * cursor;

//hand initialization

cursor->bVisible = FALSE;

if(!SetConsoleCursorInfo(hand, cursor)
    //exception output, exit(1);

it compiles but when I run it it says that it has performed an illegal operation and will be shut down.  When I go to the debugger it says, and I quote:

"unhandled exception in file.exe : 0xC000005 : Access Violation"

Can you tell me what this means?

Am I remembering wrong, or weren't you working in DOS just a few months ago.

Anyway the problem is that the second parameter to SetConsoleCursorInfo() is a pointer to a CONSOLE_CURSOR_INFO structure that you use to pass information about the cursor you want.  You are passing a pointer that has not been initalized, that is, it doesn't really point to a CONSOLE_CURSOR_INFO structure.  That is bad.

Instead declare a CONSOLE_CURSOR_INFO structure.  Fill it in and pass a pointer to it.   Make sense?
See I thought thats what I was making
what did

CONSOLE_CURSOR_INFO * cursor;

do?
I was working with console programs.  Still am.
Then what did

CONSOLE_CURSOR_INFO * cursor;

do?

 And why did it compile if its not pointing to a CONSOLE_CURSOR_INFO structure?
This is a little hard to explain.  I take it you are not too familiar with pointers. the statement

CONSOLE_CURSOR_INFO * cursor;

declares a pointer that CAN point to a CONSOLE_CURSOR_INFO.  That doesn't mean it does.  You have to make it point to the the thing you want it to point to like  In the following code cursor starts out uninitialized so it points somewhere random.  Then I change its value 3 times.  Twice it points to two different cursor info structures.

CONSOLE_CURSOR_INFO CursorInfo1;
CONSOLE_CURSOR_INFO CursorInfo2;
CONSOLE_CURSOR_INFO * cursor; // cursor points randomly at memory.

cursor = NULL;  // Cursor now points "nowehere".
cursor = &CursorInfo1; // Cursor now points at CursorInfo 1;
cursor = &CursorInfo2; // Cursor now points at CursorInfo2;

In your case you can drop the cursor variable completely.  just declare a structure of type CONSOLE_CURSOR_INFO.  Then pass a pointer to it using

CONSOLE_CURSOR_INFO &CursorInfo;  
// Initialize CursorInfo.
SetConsoleCursorInfo(hand, &CursorInfo)

You probably had better get an introductory C++ book and look at the chapters on pointers.  They are essential!
That was poor sportsmanship, Todd.

Booth882, The dark side == Microsoft (reference taken from the StarWars trilogy).
Poor sportsmanship?  for what?
Oh, you've got a point.  Didn't really think of it. Booth the original answer was Alex's. and he deserves the points.  I guess I was thinking about the new question and the fact that I really wanted to go to bed and had like 20 E-mails come in!
>> Didn't really think of it
Well, we're only human.  I see it was not intentional so apology accepted, points waved, lat's continue with our business.

>> and the fact that I really wanted to go to bed and had like 20 E-mails come in!
He he.  1:30 AM I went to sleep only to get up 20 minutes later because her royal majesty (4 months old) decided that my shoulder is more comfortable than her bed...

thats true.  I love you, nietod, but I dont approve of your tactics.  You always answer questions before you actually answer them just so no one will beat you to it.  I know you want the points, but you should be fair to the other experts.  Also it blocks them out in case they had something useful to say.  They can still comment, but there is nothing in it for them, so they usually dont.  But as for this question I think alex is right.  Although you were helpful with the pointer I think he really does deserve the points.  So if alex would please post an answer I can award him his points.
I agree that alex deserves the points.  This morning I didn't even know why he was "complaining" because I didn't remember answering the question.  Iniitially it hadn't been my intention to answer the question--at least not until I knew what type of program you were writing.   Then I guess in the "heat of the moment" I answered it without really thinking about it.

As far as "my tactic"  Its not mine.  I learned it from someone else and more experts are using it all the time.  I believe it is the fairest way to handle it.  It prevents two experts from typing long answers at the same time and only one getting the points.  It allows experts to provide complete, detailed, and well-thought out answers, rather than rushing out an incomplete answer.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You shouldn't say stuff like that.  It goes to my head.  

thanks,
I understand, forgive me nietod.  Thank you for all of your help, both of you.