• 0

[c] Moving the cursor in console mode


Question

I'm writing a simple game for the Windows 2000 command prompt - the biggest problem at present is that when I need to update one character anywhere on the screen, I need to redraw the entire screen resulting in serious flicker.

There must be some way to move the cursor to a given x,y co-ordinate on the screen. Anyone know how this is done?

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

here's the declaration, its not standard i believe but it may be found in conio or in pc. I know its in djgpp (gcc for dos) atleast.

void ScreenSetCursor(int row, int column);

Link to comment
Share on other sites

  • 0

You can use the api setcursorpos on vb

and also the getcursorpos the get the current mouse x and y

Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

Call SetCursorPos(x, y)

Public Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long

Public Type PointAPI

X As Long

Y As Long

End Type

dim mouse as pointapi

'mouse.x returns the current mouse x

'mouse.y returns the current mouse y

Link to comment
Share on other sites

  • 0

underscorebios, what are you talking about? That looks like:

  • Visual Basic, not C
  • Referring to mouse position, not the cursor
  • For the Windows APIs, not for DOS style console programs
  • For getting the position rather than moving it

???

Link to comment
Share on other sites

  • 0

I've discovered this:

SetConsoleCursorPosition

The SetConsoleCursorPosition function sets the cursor position in the specified console screen buffer.

BOOL SetConsoleCursorPosition(

 HANDLE hConsoleOutput,  // handle to console screen buffer

 COORD dwCursorPosition   // new cursor position coordinates

);

Parameters

hConsoleOutput

Handle to a console screen buffer. The handle must have GENERIC_WRITE access.

dwCursorPosition

Specifies a COORD structure containing the new cursor position. The coordinates are the column and row of a screen buffer character cell. The coordinates must be within the boundaries of the screen buffer.

I can get this added so it compiles, but I can't run it because I don't know what it means for the first paramater. What is handle? How do I get the handle for the DOS console the program is being run on?

Link to comment
Share on other sites

  • 0

Woohoo!

weenur, that page gave a little bit more info on the function I found, including a link to how to get the handle. For people who actually use the search function, here's how it's done:

/* A simple program to move the cursor */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main( void )
{
	COORD newPosition = { 0, 0 };

	system("cls");

	printf("Hello World\n");

	SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), newPosition );

	printf("!\n");

	return 0;
}

This will clear the screen, print Hello World and then writing a pling ("!") over the H. Change newPosition.X and newPosition.Y to change the position on the screen...

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.