Page 1 of 1

Custom console (WinApi)

Posted: Wed Jul 15, 2020 5:44 pm
by Mijikai
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:

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

Re: Custom console (WinApi)

Posted: Wed Jul 22, 2020 5:28 pm
by Kwai chang caine
Thanks MIJIKAY 8)
But i have a polik error with SetCurrentConsoleFontEx_ :(

Re: Custom console (WinApi)

Posted: Wed Jul 22, 2020 6:24 pm
by Mijikai
Forgot to mention that the code is for x64.

Re: Custom console (WinApi)

Posted: Thu Jul 23, 2020 5:57 pm
by Kwai chang caine
Aaaahhh !!! Pity :cry:
Thanks for your answer :wink:

Re: Custom console (WinApi)

Posted: Thu Jul 23, 2020 6:44 pm
by infratec
For x86 and x64:

Code: Select all

Import "kernel32.lib"
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    GetConsoleWindow_.i() As "_GetConsoleWindow@0"
    SetCurrentConsoleFontEx_.i(hConsoleOutput.i, bMaximumWindow.i, *lpConsoleCurrentFontEx) As "_SetCurrentConsoleFontEx@12"
  CompilerElse
    GetConsoleWindow_.i() As "GetConsoleWindow"
    SetCurrentConsoleFontEx_.i(hConsoleOutput.i,bMaximumWindow.i,*lpConsoleCurrentFontEx) As "SetCurrentConsoleFontEx"
  CompilerEndIf
EndImport
As main loop:

Code: Select all

Repeat
  key$ = Inkey()
  If key$ <> ""
    If key$ = #CR$
      PrintN("")
    Else
      Print(key$)
    EndIf
  Else
    Delay(10)
  EndIf
Until key$ = #ESC$
But the font size and the console size are not independent :!:

Re: Custom console (WinApi)

Posted: Sat Jul 25, 2020 7:13 pm
by Kwai chang caine
Yeees !!!!! thanks a lot INFRATEC for this adding 8)
I can see now the great job of MIJIKAI and that works here

Thanks at you two for sharing 8)