Thread: gotoxy API call in MS VC++, pauses?!?!

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    gotoxy API call in MS VC++, pauses?!?!

    i was dissapointed when i found that MS visual c++ doesn't have a built in gotoxy function. how is someone supposed to manipulate the console without it?

    anyway, i found the API call which uses windows.h header, but it is weird, i have this code:

    Code:
    void gotoxy(int x, int y) 
    { 
    HANDLE hConsoleOutput; 
    COORD dwCursorPosition; 
    
    dwCursorPosition.X = x; 
    dwCursorPosition.Y = y; 
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition); 
    }
    
    void main()
    {
    	gotoxy(5,5);
    	cout << "Hello there!";
    }
    but before it writes hello there, it moves the cursor to 5,5 and it just sits there blinking, until i press a key, then it continues.

    why does this happen? why did they have to make it so hard on people?

    any ideas would be nice
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    try putting <<endl after "hello there"
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    you beauty

    it works fine now thanks

    another thing
    any idea how to hide the flashing cursor?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I want to be able to set the visibility of the cursor in the console, so i looked in wincon.h, where i had previously found "SetConsoleCursorPosition()". This is what i found:

    Code:
    SetConsoleCursorInfo(
        HANDLE hConsoleOutput,
        CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo
        );
    and the structure earlier on:

    Code:
    typedef struct _CONSOLE_CURSOR_INFO {
        DWORD  dwSize;
        BOOL   bVisible;
    } CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;
    i tried to use this in pretty much the same way as SetConsoleCursorPosition(), which goes like this:

    Code:
    void gotoxy(int x, int y) 
    { 
    HANDLE hConsoleOutput; 
    COORD dwCursorPosition; 
    
    dwCursorPosition.X = x; 
    dwCursorPosition.Y = y; 
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition); 
    }
    My code for setting the visibility of the cursor was this:
    Code:
    void setCursor(bool visible)
    {
    HANDLE hConsoleOutput; 
    CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo.bVisible=visible;
    
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorInfo(hConsoleOutput,lpConsoleCursorInfo);
    }
    It did NOT work.

    if anyone knows about the Windows API and how to use it, can you give us a hand?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    void setcursor(bool visible, DWORD size)
    {
    	if(size == 0)
    	{
    		size = 20;	// default cursor size
    	}
    	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_CURSOR_INFO lpCursor;	
    	lpCursor.bVisible = visible;
    	lpCursor.dwSize = size;
    	SetConsoleCursorInfo(hConsole,&lpCursor);
    }
    The SetConsoleCursorInfo function doesn't like it if you don't assign a cursor size.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks that worked fine

    but why did you pass &lpCursor by reference?

    (i hope im right in remembering that & means ByRef
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    did you do vb before c++?

    when you declared lpConsoleCursorInfo, you made it a pointer by putting an asterisk(*) in front of it. this means that lpConsoleCursorInfo just contains a reference to memory address for the value. since you have to pass a reference to the function, and i didn't create a pointer, i had to use the ampersand(&) prefix to pass a reference to the function.

    i hope that was clear.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gotoxy function call
    By artalin in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 09:07 PM
  2. how do you call api calls
    By ismael86 in forum Windows Programming
    Replies: 8
    Last Post: 05-12-2002, 09:09 AM
  3. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM
  4. need an API call...
    By jjd228 in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2002, 06:13 AM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM