I wrote some functions for a little project. Maybe they are of some use for anyone:
Code: Select all
EnableExplicit
; ============================================================
; Makros:
; ConsoleHandle() ............ handle of the console window
;
; Funktionen:
; ConsoleLocation()
; X and Y position of the cursor in the console window
; Returns a long which contains position in COORD format
; ConsoleLocationX()
; X position of the cursor in the console window
; ConsoleLocationY()
; Y position of the cursor in the console window
; ConsoleWidth()
; width of the console window
; ConsoleHeight()
; height of the console window
; ConsoleBufferLocation()
; X and Y position of the cursor in the console screen buffer
; Returns a long which contains position in COORD format
; ConsoleBufferLocationX()
; X position of the cursor in the console screen buffer
; ConsoleBufferLocationY()
; Y position of the cursor in the console screen buffer
; ConsoleBufferWidth()
; width of the console screen buffer
; ConsoleBufferHeight()
; height of the console screen buffer
; ConsoleBufferLocate()
; similar to ConsoleLocate() but positions the cursor
; inside the console screen buffer
; ConsoleMoveUp()
; moves the cursor up one line
; sets the cursor to the left postion
; ConsoleMoveUp( CountLines )
; moves the cursor up [CountLines] lines
; sets the cursor to the left postion
; ConsoleDeletePrevLines()
; moves the cursor up one line
; sets the cursor to the left postion
; deletes the whole Line (overwrite With spaces)
; ConsoleDeletePrevLines( CountLines )
; moves the cursor up [CountLines] lines
; sets the cursor to the left postion
; deletes the whole Line (overwrite with spaces)
; GetConsoleTitle()
; returns a string, which contains the console title
;
; ============================================================
Macro ConsoleHandle()
GetStdHandle_( #STD_OUTPUT_HANDLE ) ; GetConsoleWindow_() funktioniert nicht
EndMacro
Structure tConsole_COORD
StructureUnion
coord.COORD
long.l
EndStructureUnion
EndStructure
Procedure.l ConsoleLocation()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
Protected location.tConsole_COORD
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
location\coord\x = ConsoleBufferInfo\dwCursorPosition\x - ConsoleBufferInfo\srWindow\left
location\coord\y = ConsoleBufferInfo\dwCursorPosition\y - ConsoleBufferInfo\srWindow\top
ProcedureReturn location\long
EndProcedure
Procedure ConsoleLocationX()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwCursorPosition\x - ConsoleBufferInfo\srWindow\left
EndProcedure
Procedure ConsoleLocationY()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwCursorPosition\y - ConsoleBufferInfo\srWindow\top
EndProcedure
Procedure.l ConsoleBufferLocation()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
Protected location.tConsole_COORD
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
location\coord\x = ConsoleBufferInfo\dwCursorPosition\x
location\coord\y = ConsoleBufferInfo\dwCursorPosition\y
ProcedureReturn location\long
EndProcedure
Procedure ConsoleBufferLocationX()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwCursorPosition\x
EndProcedure
Procedure ConsoleBufferLocationY()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwCursorPosition\y
EndProcedure
Procedure ConsoleWidth()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\srWindow\right - ConsoleBufferInfo\srWindow\left + 1
EndProcedure
Procedure ConsoleHeight()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\srWindow\bottom - ConsoleBufferInfo\srWindow\top + 1
EndProcedure
Procedure ConsoleBufferWidth()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwSize\x
EndProcedure
Procedure ConsoleBufferHeight()
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
ProcedureReturn ConsoleBufferInfo\dwSize\y
EndProcedure
Procedure ConsoleMoveUp( CountLines = 1 )
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole, x, y
Protected location.tConsole_COORD
If CountLines < 1 : ProcedureReturn #False : EndIf
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
location\coord = ConsoleBufferInfo\dwCursorPosition
location\coord\x = 0
location\coord\y - CountLines
If location\coord\y < 0 : location\coord\y = 0
ElseIf location\coord\y >= ConsoleBufferInfo\dwSize\y : location\coord\y = ConsoleBufferInfo\dwSize\y - 1 : EndIf
SetConsoleCursorPosition_( hConsole, location\long )
ProcedureReturn #True
EndProcedure
Procedure ConsoleDeletePrevLines( CountLines = 1 )
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole, x, y
Protected location.tConsole_COORD
If CountLines < 1 : ProcedureReturn #False : EndIf
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
location\coord\x = 0
location\coord\y = ConsoleBufferInfo\dwCursorPosition\y
While CountLines And location\coord\y
location\coord\y - 1
SetConsoleCursorPosition_( hConsole, location\long )
Print( Space(ConsoleBufferInfo\dwSize\x) )
If CountLines = 1
SetConsoleCursorPosition_( hConsole, location\long )
EndIf
CountLines - 1
Wend
ProcedureReturn #True
EndProcedure
Procedure ConsoleBufferLocate( x, y )
Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
Protected hConsole
Protected location.tConsole_COORD
If y < 0 Or y < 0
; x or y outside the console screen buffer
ProcedureReturn #False
EndIf
hConsole = ConsoleHandle()
GetConsoleScreenBufferInfo_( hConsole, @ConsoleBufferInfo )
If y >= ConsoleBufferInfo\dwSize\y Or x >= ConsoleBufferInfo\dwSize\x
; x or y outside the console screen buffer
ProcedureReturn #False
EndIf
location\coord\x = x
location\coord\y = y
SetConsoleCursorPosition_( hConsole, location\long )
ProcedureReturn #True
EndProcedure
Procedure.s GetConsoleTitle()
Protected title.s = Space(1024)
GetConsoleTitle_( @title, 1024 )
ProcedureReturn title
EndProcedure
cu, guido
Code: Select all
Macro WaitKey()
While Inkey() = "" : Delay(100) : Wend
EndMacro
Define i
OpenConsole()
For i = 0 To 100
PrintN( Str(i) )
Next i
WaitKey()
ConsoleMoveUp( 10 )
For i = 1001 To 1010
PrintN( "Overwrite with " + Str(i) )
Next i
WaitKey()
ConsoleDeletePrevLines( 10 )
PrintN( "Deleted last 10 lines" )
WaitKey()
ConsoleMoveUp( 20 )
PrintN( "Overwrite with something else ..." )
WaitKey()
ConsoleBufferLocate( 0, 0 )
Print( " <-- This is position (0,0) in screen buffer" )
ConsoleBufferLocate( 0, 0 )
WaitKey()
ConsoleBufferLocate( 0, ConsoleBufferHeight()-1 )
Print( RSet("This is position (" + Str(ConsoleBufferWidth()-1) + "," + Str(ConsoleBufferHeight()-1) + ") in screen buffer -->", ConsoleBufferWidth()-1, " "))
WaitKey()
CloseConsole()
End