Print (font) size

New to FreeBASIC? Post your questions here.
marquijr
Posts: 1
Joined: May 26, 2013 18:41

Print (font) size

Postby marquijr » May 26, 2013 20:13

What is the code for adjusting the size of print in the console?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Print (font) size

Postby MichaelW » May 27, 2013 5:43

Assuming that you are running Windows, AFAIK changing the console font size from FreeBASIC code will require that the code modify the registry, probably not a good idea. At least for Windows 2000/XP you can change the font size in the console properties, by selecting the Lucida Console font and a size, then apply the changes to the current console window or future console windows with the same title.
I3I2UI/I0
Posts: 90
Joined: Jun 03, 2005 10:39
Location: Germany

Re: Print (font) size

Postby I3I2UI/I0 » May 27, 2013 8:30

Code: Select all

#Include Once "windows.bi"
'ab Windows Vista
Function Set_ConsoleFont(typ As Integer) As Integer
  Dim As Any Ptr Kernel32
  Dim As Integer tmp
  Dim SetConsoleFont As Function(ByVal As HANDLE, ByVal As Integer) As Integer
  Kernel32 = DylibLoad("Kernel32.dll")
  If Kernel32 Then
    SetConsoleFont = DyLibSymbol(Kernel32,"SetConsoleFont")
    tmp = SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),typ)
    DyLibFree Kernel32
    Function = tmp
  EndIf
End Function

Function NumberOfFonts() As Integer
  Dim As Any Ptr Kernel32
  Dim As Integer tmp
  Dim GetNumberOfConsoleFonts As Function() As Integer
  Kernel32 = DylibLoad("Kernel32.dll")
  If Kernel32 Then
    GetNumberOfConsoleFonts = DyLibSymbol(Kernel32,"GetNumberOfConsoleFonts")
    tmp = GetNumberOfConsoleFonts()
    DyLibFree Kernel32
    Function = tmp
  EndIf
End Function

Width 50,24: Color 15,1: Cls
For i As Integer = 16 To 254
  Print Chr(i);
Next
Print
For n As Integer = 1 To NumberOfFonts
  Sleep 2000,1
  Set_ConsoleFont(n-1)
Next
Sleep

or
http://www.freebasic-portal.de/code-beispiele/windows-gui/schriftart-in-der-konsole-aendern-232.html
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Print (font) size

Postby MichaelW » May 27, 2013 16:35

Interesting. On my WindowXP SP3 system kernel32.dll exports these relevant (and currently undocumented AFAIK) functions:

Code: Select all

293 124 00074951 GetConsoleFontInfo
393 188 000756B9 GetNumberOfConsoleFonts
741  2E4 0007519D SetConsoleFont

And all of these functions are included in the 0.24.0 kernel32.dll import library, so all you actually need is the function declarations. My declarations for the undocumented functions are based on guesswork, but they appear to be OK.

Code: Select all

''=============================================================================
#include "windows.bi"
''=============================================================================

type PCONSOLE_FONT_INFO as CONSOLE_FONT_INFO ptr

extern "Windows"
    declare function GetConsoleFontInfo( byval as HANDLE, _
                                byval as BOOL, _
                                byval as DWORD, _
                                byval as PCONSOLE_FONT_INFO ) as BOOL

    declare function GetCurrentConsoleFont( byval as HANDLE, _
                                byval as BOOL, _
                                byval as PCONSOLE_FONT_INFO ) as BOOL

    declare function GetNumberOfConsoleFonts() as DWORD

    declare function SetConsoleFont( byval as HANDLE, _
                                     byval as DWORD ) as BOOL
end extern

''=============================================================================

dim as integer nFonts
dim as HANDLE hStdOut

nFonts = GetNumberOfConsoleFonts()

dim as CONSOLE_FONT_INFO cfi(nFonts)

print nFonts
print

hStdOut = GetStdHandle( STD_OUTPUT_HANDLE )

GetCurrentConsoleFont( hStdOut, true, @cfi(0) )
print cfi(0).nFont, cfi(0).dwFontSize.x, cfi(0).dwFontSize.y
print

print SetConsoleFont( hStdOut, 0 )
print

GetCurrentConsoleFont( hStdOut, true, @cfi(0) )
print cfi(0).nFont, cfi(0).dwFontSize.x, cfi(0).dwFontSize.y
print

print GetConsoleFontInfo( hStdOut, true, nFonts, @cfi(0) )

for i as integer = 0 to nFonts-1
    print cfi(i).nFont, cfi(i).dwFontSize.x, cfi(i).dwFontSize.y
next

print

for i as integer = 0 to nFonts-1
    SetConsoleFont( hStdOut, i )
    sleep 2000
next

print SetConsoleFont( hStdOut, 0 )
print
print "done"
sleep

Note that GetCurrentConsoleFont has been documented for a long time, but for some reason the declaration is not included in the FreeBASIC header files.

Note also that starting with Vista there is a documented GetCurrentConsoleFontEx function that provides more information, including the Face Name for example:

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests