[Windows] Funktionen für das Konsolenfenster

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
helpy
Beiträge: 636
Registriert: 29.08.2004 13:29

[Windows] Funktionen für das Konsolenfenster

Beitrag von helpy »

Hallo zusammen,

Für ein kleines Projekt habe ich ein paar Funktionen gebraucht. Vielleicht kann sie ja jemand von Euch brauchen:

Code: Alles auswählen

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
Kleines Testprogramm:
cu, guido

Code: Alles auswählen

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
Zuletzt geändert von helpy am 28.06.2010 15:55, insgesamt 2-mal geändert.
Windows 10
PB Last Final / (Sometimes testing Beta versions)
Benutzeravatar
PureLust
Beiträge: 1145
Registriert: 21.07.2005 00:02
Computerausstattung: Hab aktuell im Grunde nur noch 'nen Lenovo Yoga 2 Pro im Einsatz.
Wohnort: am schönen Niederrhein

Re: [Windows] Funktionen für das Konsolenfenster

Beitrag von PureLust »

Sieht auf den ersten Blick schon mal sehr interessant und hilfreich aus. :allright:

Da kein lauffähiges Beispiel dabei ist noch eine kurze Frage dazu:
Braucht man dazu eine Console im Grafikmodus (also per EnableGraphicalConsole() ), oder funktioniert diese Geschichte auch im reinen Text-Modus?

Thx und Gruß, PL.
[Dynamic-Dialogs] - komplexe dynamische GUIs einfach erstellen
[DeFlicker] - Fenster flimmerfrei resizen
[WinFX] - Window Effekte (inkl. 'durchklickbares' Window)
Benutzeravatar
helpy
Beiträge: 636
Registriert: 29.08.2004 13:29

Re: [Windows] Funktionen für das Konsolenfenster

Beitrag von helpy »

Hallo PureLust,

Nur im reinen Textmodus!

Grafikmodus habe ich nicht getestet!

cu, guido

PS: ConsoleDeletePrevLines() ist noch nicht fehlerfrei ... werde ich noch umschreiben ...
Windows 10
PB Last Final / (Sometimes testing Beta versions)
Benutzeravatar
PureLust
Beiträge: 1145
Registriert: 21.07.2005 00:02
Computerausstattung: Hab aktuell im Grunde nur noch 'nen Lenovo Yoga 2 Pro im Einsatz.
Wohnort: am schönen Niederrhein

Re: [Windows] Funktionen für das Konsolenfenster

Beitrag von PureLust »

helpy hat geschrieben:Hallo PureLust,

Nur im reinen Textmodus!
Suppie ... Thx. :allright:
[Dynamic-Dialogs] - komplexe dynamische GUIs einfach erstellen
[DeFlicker] - Fenster flimmerfrei resizen
[WinFX] - Window Effekte (inkl. 'durchklickbares' Window)
Antworten