The MASM Forum

General => The Campus => Topic started by: frktons on December 18, 2012, 09:48:28 AM

Title: Changing the font and the font size of a console via API
Post by: frktons on December 18, 2012, 09:48:28 AM
The console fonts and size of the font are somewhat a mistery
for newbie like me. Many times I had the desire to change
font or font size of the console, via program, but didn't
find how to do it.
Now I've found some hint:


    const int STD_OUT_HANDLE = -11;

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern int SetConsoleFont(
        IntPtr hOut,
        uint dwFontNum
        );

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetStdHandle(int dwType);

    Console.WriteLine("This is an error!");
    Console.ReadLine();
    SetConsoleFont(GetStdHandle(STD_OUT_HANDLE), 9);
    Console.ReadLine();


I think it is "C" or one of his relatives.
Could somebody tell me how to translate this "dll export"
into masm32, so that the non-documented API becomes
a documented one?

Thanks
Title: Re: Changing the font and the font size of a console via API
Post by: Donkey on December 18, 2012, 09:57:11 AM
Just use GetModuleHandle and GetProcAddress if the function is not in the standard MASM32 libs. Using that method you can be assured of the portability of your code without custom libraries:

szKern32 DB "Kernel32.dll",0
szSetConsoleFont DB "SetConsoleFont",0

invoke GetModuleHandle,ADDR szKern32,0
; no need to save the handle Kernel32.dll is already loaded and will be unloaded automatically
invoke GetProcAddress, eax, ADDR szSetConsoleFont
mov pSetConFont, eax


Note that there is also SetConsoleFontSize to set the size

For GoAsm there is no need for GetModuleHandle etc...

invoke Kernel32.dll:SetConsoleFont, [hConsole], [Index]
Title: Re: Changing the font and the font size of a console via API
Post by: frktons on December 18, 2012, 10:04:25 AM
Thanks Edgar. I searched the include path of masm32
and I found:

SetConsoleFont PROTO STDCALL :DWORD,:DWORD

in kernl32p.inc.

Does it mean that I can use it like any other API?

And SetConsoleFontSize is not in any include file...
Title: Re: Changing the font and the font size of a console via API
Post by: Donkey on December 18, 2012, 10:07:37 AM
As long as there is a LIB file for that include (kernl32p.lib) then you have just to include the INC and LIB files and the function will be available as far as I know, it has been many many years since I used MASM.
Title: Re: Changing the font and the font size of a console via API
Post by: jj2007 on December 18, 2012, 05:05:38 PM
I used to have SetConsoleFont in MasmBasic to get wPrint running, but it depends on HKEY_CURRENT_USER/Console, and that is a fairly unreliable place. SetConsoleFont works only when you know the index of the font, and at that point rather than telling the user "go to the registry and find out where Lucida Console is located", you are better of letting him find the console properties ;-)
Title: Re: Changing the font and the font size of a console via API
Post by: MichaelW on December 18, 2012, 07:19:18 PM
Another possibility is to code your app to change the registry setting and then restart the console.
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 18, 2012, 09:49:32 PM
or - you could read the registry and see what fonts/sizes are available   :P

try to select a size that appears on all machines
Title: Re: Changing the font and the font size of a console via API
Post by: jj2007 on December 18, 2012, 09:55:10 PM
Have a look at HKEY_CURRENT_USER/Console - it's really hi tech, you will like it :greensml:
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 18, 2012, 10:13:55 PM
i see
well - that being the case, just enumerate "Terminal" fonts and pick a size
i think you'll find there is a certain minimum level of support on machines with win 2000 or newer
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 18, 2012, 10:20:31 PM
here is a little program   :P

XP MCE2005 SP3
8 x 12
4 x 6
5 x 12
6 x 8
7 x 12
10 x 18
12 x 16
8 x 12
16 x 12
8 x 8
16 x 8


8 x 12 always gets enumerated twice - lol
might be because that's the one in use
Title: Re: Changing the font and the font size of a console via API
Post by: jj2007 on December 18, 2012, 11:20:52 PM
Nice, Dave :t
Attached an urgently needed add-on ;-)
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 18, 2012, 11:27:02 PM
the only one i get for lucida console is 19 x 23 (because it is the only one with OEM_CHARSET)
that is a true-type font - not sure you can select that into the console
i think it has to be a mono-space font

that one size may be a mono-space font
OEM_CHARSET means it has the line graphics characters - and they only make sense in mono-space
Title: Re: Changing the font and the font size of a console via API
Post by: jj2007 on December 19, 2012, 12:02:01 AM
The Lucida appears for 0 and for OEM_CHARSET, while the terminal fonts appear only for OEM_CHARSET.

> Lucide .. is a true-type font - not sure you can select that into the console
You have to if you want to see Unicode
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 19, 2012, 12:03:52 AM
i think if you set it to 0, it enumerates all types that match the other criteria

QuoteYou have to if you want to see Unicode
then, it must be mono-space   :biggrin:

unfortunately, they didn't give you a good way to enumerate "all mono-space" fonts, per se
you have to weed them out
it only looks at what you set for lfCharSet, lfFaceName, and lfPitchAndFamily
the other members of the LOGFONT structure are ignored for EnumFontFamiliesEx
maybe one of the other font enumeration functions would be better suited
i used EnumFontFamiliesEx because i just wanted Terminal font sizes with OEM_CHARSET
Title: Re: Changing the font and the font size of a console via API
Post by: Vortex on December 19, 2012, 05:17:54 AM
Changing Console Fonts :

http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx
Title: Re: Changing the font and the font size of a console via API
Post by: jj2007 on December 19, 2012, 05:26:38 AM
Quote from: Vortex on December 19, 2012, 05:17:54 AM
Changing Console Fonts :

http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx

Good link. I thought of using Dave's enumeration together with SetConsoleFont, but it is still unclear to me how to get from the enum to having the index that SetConsoleFont asks for.
Title: Re: Changing the font and the font size of a console via API
Post by: Donkey on December 19, 2012, 05:39:38 AM
Quote from: jj2007 on December 19, 2012, 05:26:38 AM
Good link. I thought of using Dave's enumeration together with SetConsoleFont, but it is still unclear to me how to get from the enum to having the index that SetConsoleFont asks for.

Tough call since Microsoft has been actively eliminating everything that allows you to manipulate the console fonts programmatically since Windows 2000. If you do find a way there is little guarantee that it will survive a service pack let alone a version upgrade. Microsoft decided years ago that allowing programmatic changes introduced far too much instability in the console has been removing it.

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q247815

QuoteIn Windows 2000, the installation of Console Fonts is no longer automated. This was done to give the console window greater stability in multilanguage environments.
Title: Re: Changing the font and the font size of a console via API
Post by: dedndave on December 19, 2012, 05:49:20 AM
my enum code uses EnumFontFamiliesEx
that may not be the best choice for what you want to do

there is EnumFonts(Ex) and, perhaps, GetFontData
beyond that, you may have to select a font into a DC in order to get more info

as you can see from the lucida console font, true-type font files can embed mono-space fonts
the font is both true-type and mono-space   :P
if you get really interested, you can research info on how to create font files

personally, i think it is way too much effort to put into a console app - lol