[Windows] functions for console window

Share your advanced PureBasic knowledge/code with the community.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

[Windows] functions for console window

Post by helpy »

Hello PureBasic user,

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
Little test program:
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
cu, guido
Last edited by helpy on Mon Jun 28, 2010 2:56 pm, edited 2 times in total.
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Windows] functions for console window

Post by Kwai chang caine »

Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: [Windows] functions for console window

Post by RichAlgeni »

Thank you Guido! Finally got back to look at this.

Question you may know the answer to: Is there a way to programatically change the width and size of a console window? For instance, you want to change the width from 80, to 132?

Thanks!

Rich
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: [Windows] functions for console window

Post by jassing »

RichAlgeni wrote:Thank you Guido! Finally got back to look at this.

Question you may know the answer to: Is there a way to programatically change the width and size of a console window? For instance, you want to change the width from 80, to 132?

Thanks!

Rich
generally bad form to ask the question multiple times in the same day -- also doesn't help users searching for a similar solution... (for them, see the windows api SetConsoleWindowInfo())
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: [Windows] functions for console window

Post by Zebuddi123 »

Thank you for sharing helpy very useful

Zebuddi. :D
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: [Windows] functions for console window

Post by RichAlgeni »

generally bad form to ask the question multiple times in the same day -- also doesn't help users searching for a similar solution... (for them, see the windows api SetConsoleWindowInfo())
You're right, I screwed up! My apologies. Just completely spaced it! Won't happen again.
Post Reply