4

I just wrote the code for tower of hanoi in c and I wanted to show the solution in graphical mode using characters.

I want to use windows.h and SetConsoleCursorPosition function to move cursor in console.

Could you help me by telling me have does this function works and how to use it?Please give some examples.

rekire
  • 47,260
  • 30
  • 167
  • 264
A7A
  • 139
  • 1
  • 4
  • 11

1 Answers1

9

Here's an example of how to call the SetConsoleCursorPosition function, taken from cplusplus:

void GoToXY(int column, int line)
{
    // Create a COORD structure and fill in its members.
    // This specifies the new position of the cursor that we will set.
    COORD coord;
    coord.X = column;
    coord.Y = line;

    // Obtain a handle to the console screen buffer.
    // (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
    // in conjunction with the GetStdHandle() to retrieve the handle.)
    // Note that because it is a standard handle, we don't need to close it.
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // Finally, call the SetConsoleCursorPosition function.
    if (!SetConsoleCursorPosition(hConsole, coord))
    {
        // Uh-oh! The function call failed, so you need to handle the error.
        // You can call GetLastError() to get a more specific error code.
        // ...
    }
}

You can also find out how to use Win32 functions by checking the SDK documentation. Googling for the name of the function will usually turn up the appropriate doc page as the first hit.
For SetConsoleCursorPosition, the page is here, and for GetStdHandle, the page is here.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Minor nit: but google search results ordering is different for everyone, based upon their search history. – Randy Howard Apr 02 '13 at 18:01
  • I delete my cookies almost daily. – rekire Apr 02 '13 at 18:03
  • cplusplus? What a weird place to go looking for information about a Win32 function. What would be wrong with [the documentation for the function he asks about](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025.aspx)? – Cody Gray - on strike Apr 02 '13 at 18:10
  • There is no example and this is not simple for beginners. But in general you are right the API documentation is always a good point to start. – rekire Apr 02 '13 at 18:13
  • Thanks.How can i use this graphically?I mean to show something?In here for the moves we should have for the Tower Of Hanoi. – A7A Apr 02 '13 at 19:32
  • Well jump to position 0,0 and override all with spaces. – rekire Apr 02 '13 at 19:40
  • Sorry to bother you, but could you please give me some examples? I had no idea about working with this function before.For example i want to say move from left to right for the Tower problem and i want to show it using letters L and R to show it.can you suggest something? – A7A Apr 02 '13 at 19:49
  • Try the [code from Jerry Coffin](http://stackoverflow.com/a/5866648/995926). This should clear your console. Than you can *build* your image new. – rekire Apr 02 '13 at 19:56