News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Changing the font and the font size of a console via API

Started by frktons, December 18, 2012, 09:48:28 AM

Previous topic - Next topic

frktons

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
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

Donkey

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]
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

frktons

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...
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

Donkey

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.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

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 ;-)

MichaelW

Another possibility is to code your app to change the registry setting and then restart the console.
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

or - you could read the registry and see what fonts/sizes are available   :P

try to select a size that appears on all machines

jj2007

Have a look at HKEY_CURRENT_USER/Console - it's really hi tech, you will like it :greensml:

dedndave

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

dedndave

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

jj2007


dedndave

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

jj2007

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

dedndave

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

Vortex

Changing Console Fonts :

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