SetConsoleCursorPosition function (Windows)

Switch View :
ScriptFree
SetConsoleCursorPosition function

Applies to: desktop apps only

Sets the cursor position in the specified console screen buffer.

Syntax

BOOL WINAPI SetConsoleCursorPosition(
  __in  HANDLE hConsoleOutput,
  __in  COORD dwCursorPosition
);

Parameters

hConsoleOutput [in]

A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.

dwCursorPosition [in]

A COORD structure that specifies the new cursor position, in characters. The coordinates are the column and row of a screen buffer character cell. The coordinates must be within the boundaries of the console screen buffer.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The cursor position determines where characters written by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function, are displayed. To determine the current position of the cursor, use the GetConsoleScreenBufferInfo function.

If the new cursor position is not within the boundaries of the console screen buffer's window, the window origin changes to make the cursor visible.

Examples

For an example, see Using the High-Level Input and Output Functions.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Wincon.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Console Functions
Console Screen Buffers
GetConsoleCursorInfo
GetConsoleScreenBufferInfo
ReadConsole
ReadFile
SetConsoleCursorInfo
WriteConsole
WriteFile

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Community Content

C0n_z0L
IMPORTANT: SetConsoleCursorPosition fails EVERY TIME under Visual Basic. SOLUTION:
<citation>

...As for positioning the cursor, yes you can. You need to use the SetConsoleCursorPosition API. But beware, it is not correctly defined in the Win32API.

The reason the SetConsoleCursorPosition function fails is it is declared INCORRECTLY in the API text viewer. It is defined like this:

Declare Function SetConsoleCursorPosition _
Lib "kernel32" (ByVal hConsoleOutput As Long, _
dwCursorPosition As COORD) As Long

The problem is, the 2nd argument "dwCursorPosition" MUST be passed ByVal, not ByRef. If you don't specify ByVal, ByRef is the default in Visual Basic... HOWEVER... VB will not let you pass a user-defined type ByVal.

This means you have to define the function like this:

Declare Function SetConsoleCursorPosition _
Lib "kernel32" (ByVal hConsoleOutput As Long, _
ByVal dwCursorPosition As Long) As Long

Now, you have to fit the X/Y values that used to be assigned with the COORD type into a Long type. You can do this by adding this function to your module:

Function cvtCoordToLng(wHi As Integer, wLo As Integer) As Long
cvtCoordToLng = (wHi * &H10000) Or (wLo And &HFFFF&)
End Function


Then when you call the SetConsoleCursorPosition API, you want to do it like this:

Dim Row As Integer
Dim Col As Integer
Dim rVal As Long
'SET Row and Col to the proper values here
rVal = SetConsoleCursorPosition(hStdOut, cvtCoordToLng(Row, Col))

<end citation>
The solution is provided by "TimCottee" on "http://www.vbforums.com/archive/index.php/t-25557.html", on Aug 14th 2000.
Of course, you can use "cvtCoordToLng" function not directly, but outside the "SetConsoleCursorPosition" call and then just pass the resultant Long value as a second argument to the "SetConsoleCursorPosition" function.
The bottom line is that this solution WORKS, which proves that Win32API definition (build by Microsoft) is DEFINITELY WRONG !!!