Custom console (WinApi)
Posted: Wed Jul 15, 2020 5:44 pm
For fun i played around with the console trying to produce a usable layout/setup for games/graphics.
It seemed i was getting somewhere but after some testing it was not reliable and i gave up.
Anyway here is the code to setup a custom console:
It seemed i was getting somewhere but after some testing it was not reliable and i gave up.
Anyway here is the code to setup a custom console:
Code: Select all
EnableExplicit
Import "kernel32.lib"
GetConsoleWindow_.i() As "GetConsoleWindow"
SetCurrentConsoleFontEx_.i(hConsoleOutput.i,bMaximumWindow.i,*lpConsoleCurrentFontEx) As "SetCurrentConsoleFontEx"
EndImport
Structure CONSOLE_FONT_INFOEX
cbSize.l
nFont.l
dwFontSize.COORD
FontFamily.l
FontWeight.l
FaceName.w[#LF_FACESIZE]
EndStructure
Procedure.i ConsoleFont(*Buffer,BufferSize.i,FontName.s,FontSize.i)
Protected cfiex.CONSOLE_FONT_INFOEX
Protected res.i
Protected length.i
With cfiex
length = Len(FontName)
If length < #LF_FACESIZE
\cbSize = SizeOf(CONSOLE_FONT_INFOEX)
\nFont = #Null
\dwFontSize\x = FontSize
\dwFontSize\y = FontSize
\FontFamily = #FF_DONTCARE
\FontWeight = #FW_NORMAL
If *Buffer And BufferSize
AddFontMemResourceEx_(*Buffer,BufferSize,#Null,@res)
EndIf
CopyMemory(@FontName,@\FaceName[0],length << 1)
ProcedureReturn SetCurrentConsoleFontEx_(GetStdHandle_(#STD_OUTPUT_HANDLE),#False,@cfiex)
EndIf
ProcedureReturn #False
EndWith
EndProcedure
Procedure.i CreateConsole(X.i,Y.i,FontName.s,FontSizeX.i,FontSizeY.i,FontWeight.i = #FW_DONTCARE)
Protected handle.i
Protected length.i
Protected sb.CONSOLE_SCREEN_BUFFER_INFO
Protected sr.SMALL_RECT
Protected co.COORD
Protected cfiex.CONSOLE_FONT_INFOEX
Protected mon.i
Protected mon_info.MONITORINFO
Protected wnd_rect.RECT
length = Len(FontName)
If length < #LF_FACESIZE
If OpenConsole()
handle = GetStdHandle_(#STD_OUTPUT_HANDLE)
If handle
If GetConsoleScreenBufferInfo_(handle,@sb)
sr\right = X - 1
sr\bottom = Y - 1
If SetConsoleWindowInfo_(handle,#True,@sr)
co\x = X
co\y = Y
If SetConsoleScreenBufferSize_(handle,PeekL(@co))
cfiex\cbSize = SizeOf(CONSOLE_FONT_INFOEX)
cfiex\dwFontSize\x = FontSizeX
cfiex\dwFontSize\y = FontSizeY
cfiex\FontFamily = #FF_DONTCARE
cfiex\FontWeight = FontWeight
CopyMemory(@FontName,@cfiex\FaceName[0],length << 1)
If SetCurrentConsoleFontEx_(handle,#False,@cfiex)
handle = GetConsoleWindow_()
If handle
mon = MonitorFromWindow_(handle,#MONITOR_DEFAULTTONEAREST)
If mon And GetWindowRect_(handle,@wnd_rect)
mon_info\cbSize = SizeOf(MONITORINFO)
If GetMonitorInfo_(mon,@mon_info)
mon_info\rcMonitor\right - mon_info\rcMonitor\left - (wnd_rect\right - wnd_rect\left)
mon_info\rcMonitor\bottom - mon_info\rcMonitor\top - (wnd_rect\bottom - wnd_rect\top)
SetWindowLongPtr_(handle,#GWL_STYLE,GetWindowLongPtr_(handle,#GWL_STYLE) &~ #WS_SIZEBOX &~ #WS_MAXIMIZEBOX)
MoveWindow_(handle,mon_info\rcMonitor\right >> 1,mon_info\rcMonitor\bottom >> 1,wnd_rect\right,wnd_rect\bottom,#True)
ProcedureReturn #True
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
CloseConsole()
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
CreateConsole(80,60,"Terminal",8,8);set font and size of console + center the window!
;This does work most of the time but not always!
;Also setting the font only works for very few specific fonts and sizes (check out msdn)
Input()
CloseConsole()
End