Console Font Size

This is a discussion on Console Font Size within the Windows Programming forums, part of the Platform Specific Boards category; Ok so after worrying about transparency for quite a while, I was wondering whether it's possible to decrease the consoles ...

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Console Font Size

    Ok so after worrying about transparency for quite a while, I was wondering whether it's possible to decrease the consoles font size.
    Like to 6 x 8 pixels?
    Using Raster Fonts of course.
    I want it to work on XP AND Vista though, so anything that requires Vista is a no go I guess..
    Possible?
    Impossible?
    Ideas?
    Thanks
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean programmatically, or by the "usual" method of right-clicking, selecting properties, selecting the font tab and selecting whatever size and style of font you like?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Programmatically
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Check on the Windows forum - C++ has no way of changing fonts.

  5. #5
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Umm...

    Can a mod please move this thread to windows programming?
    Thanks.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Moved to Windows programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Thankyou, laserlight.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure if it's any help, but the font size/name for cmd.exe is stored in:
    HKEY_CURRENT_USER\Console\...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    I actually had to do this once. Don't know if it works on Vista.
    Code:
    #define _WIN32_WINNT 0x0500 // for GetConsoleFontSize(), XP and up
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    //------------------------------------------------------------------------------
    
    struct CONSOLE_FONT
    {
        DWORD index;
        COORD dim;
    };//CONSOLE_FONT
    
    BOOL (WINAPI *SetConsoleFont)(HANDLE, DWORD);
    BOOL (WINAPI *GetConsoleFontInfo)(HANDLE, BOOL, DWORD, CONSOLE_FONT*);
    DWORD (WINAPI *GetNumberOfConsoleFonts)();
    
    //------------------------------------------------------------------------------
    
    template<typename pfn_t>
    inline bool LoadFunc(HMODULE hmod, const char *name, pfn_t &fn)
    {
        fn = (pfn_t)GetProcAddress(hmod, name);
        return fn != 0;
    }//LoadFunc
    
    //------------------------------------------------------------------------------
    
    int main()
    {
        char title[MAX_PATH];
        HWND hwnd;
    
        if (!::GetConsoleTitleA(title, sizeof(title)) || 
            !(hwnd = ::FindWindowA(0, title)))
        {
            cerr << "Failed to find console window" << endl;
            return 1;
        }//else
    
        // Undocumented API's
        HMODULE hmod = ::GetModuleHandleA("KERNEL32.DLL");
        if (!hmod || 
            !LoadFunc(hmod, "SetConsoleFont", SetConsoleFont) ||
            !LoadFunc(hmod, "GetConsoleFontInfo", GetConsoleFontInfo) ||
            !LoadFunc(hmod, "GetNumberOfConsoleFonts", GetNumberOfConsoleFonts))
        {
            cerr << "Failed to load API(s): " << ::GetLastError() << endl;
            return 1;
        }//if
    
        HANDLE hOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
    
        // number of console fonts
        const DWORD MAX_FONTS = 40;
        DWORD num_fonts = GetNumberOfConsoleFonts();
        if (num_fonts > MAX_FONTS)
            num_fonts = MAX_FONTS;
    
        CONSOLE_FONT fonts[MAX_FONTS] = {0};
        GetConsoleFontInfo(hOut, 0, num_fonts, fonts);
    
        for (DWORD n = 0; n < num_fonts; ++n)
        {
            fonts[n].dim = GetConsoleFontSize(hOut, fonts[n].index);
    
            if (fonts[n].dim.X == 6 && 
                fonts[n].dim.Y == 8)
            {
                SetConsoleFont(hOut, fonts[n].index);
                ::InvalidateRect(hwnd, 0, FALSE);
                ::UpdateWindow(hwnd);
                return 0;
            }//if
        }//if
    
        cerr << "Didn't find font" << endl;
        return 1;
    }//main
    gg

  10. #10
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Quote Originally Posted by Codeplug View Post
    I actually had to do this once. Don't know if it works on Vista.
    Code:
    #define _WIN32_WINNT 0x0500 // for GetConsoleFontSize(), XP and up
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    //------------------------------------------------------------------------------
    
    struct CONSOLE_FONT
    {
        DWORD index;
        COORD dim;
    };//CONSOLE_FONT
    
    BOOL (WINAPI *SetConsoleFont)(HANDLE, DWORD);
    BOOL (WINAPI *GetConsoleFontInfo)(HANDLE, BOOL, DWORD, CONSOLE_FONT*);
    DWORD (WINAPI *GetNumberOfConsoleFonts)();
    
    //------------------------------------------------------------------------------
    
    template<typename pfn_t>
    inline bool LoadFunc(HMODULE hmod, const char *name, pfn_t &fn)
    {
        fn = (pfn_t)GetProcAddress(hmod, name);
        return fn != 0;
    }//LoadFunc
    
    //------------------------------------------------------------------------------
    
    int main()
    {
        char title[MAX_PATH];
        HWND hwnd;
    
        if (!::GetConsoleTitleA(title, sizeof(title)) || 
            !(hwnd = ::FindWindowA(0, title)))
        {
            cerr << "Failed to find console window" << endl;
            return 1;
        }//else
    
        // Undocumented API's
        HMODULE hmod = ::GetModuleHandleA("KERNEL32.DLL");
        if (!hmod || 
            !LoadFunc(hmod, "SetConsoleFont", SetConsoleFont) ||
            !LoadFunc(hmod, "GetConsoleFontInfo", GetConsoleFontInfo) ||
            !LoadFunc(hmod, "GetNumberOfConsoleFonts", GetNumberOfConsoleFonts))
        {
            cerr << "Failed to load API(s): " << ::GetLastError() << endl;
            return 1;
        }//if
    
        HANDLE hOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
    
        // number of console fonts
        const DWORD MAX_FONTS = 40;
        DWORD num_fonts = GetNumberOfConsoleFonts();
        if (num_fonts > MAX_FONTS)
            num_fonts = MAX_FONTS;
    
        CONSOLE_FONT fonts[MAX_FONTS] = {0};
        GetConsoleFontInfo(hOut, 0, num_fonts, fonts);
    
        for (DWORD n = 0; n < num_fonts; ++n)
        {
            fonts[n].dim = GetConsoleFontSize(hOut, fonts[n].index);
    
            if (fonts[n].dim.X == 6 && 
                fonts[n].dim.Y == 8)
            {
                SetConsoleFont(hOut, fonts[n].index);
                ::InvalidateRect(hwnd, 0, FALSE);
                ::UpdateWindow(hwnd);
                return 0;
            }//if
        }//if
    
        cerr << "Didn't find font" << endl;
        return 1;
    }//main
    gg
    Whoa!
    Thanks for help!
    SetConsoleFontSize() isn't declared though.
    Is it my compiler, or what?
    Thanksyou CodePlug.

    EDIT: When I try it as a Win32 console it works perfectly... My other code doesn't though :/
    Last edited by bradszy; 04-23-2008 at 11:38 PM.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bradszy View Post
    Whoa!
    Thanks for help!
    SetConsoleFontSize() isn't declared though.
    Is it my compiler, or what?
    Thanksyou CodePlug.

    EDIT: When I try it as a Win32 console it works perfectly... My other code doesn't though :/
    Huh? What do you mean "when I try it as a Win32 console it works"? Are you trying to use console functions on a non-console application, or something else?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    What I did was:

    - Download something called "Implib" from sourceforge. It contains a kernel32.lib that exports all functions, including the undocumented ones. Replace your existing kernel32.lib from your "Microsoft SDKs" folder with it (don't forget to back up, just in case).
    - Add this to your wincon.h:
    BOOL WINAPI SetConsoleFont(HANDLE hConsoleOutput,DWORD dwIndex);

    hConsoleOutput - HANDLE to the console's output buffer
    dwIndex - Index of the font in the font list

    You can now use SetConsoleFont anywhere.

    There is a slightly more advanced way to change the console font that involves the code found at:

    http://www.catch22.net/source/files/setconsoleinfo.c

    (It simulates the message sent when the user changes the window properties, so you really have full access. For a standalone console, make sure you replace "PROCESS_ALL_ACCESS" with "MAX_ALLOWED" in OpenProcess() to avoid windows rejecting the message)

  13. #13
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Quote Originally Posted by matsp View Post
    Huh? What do you mean "when I try it as a Win32 console it works"? Are you trying to use console functions on a non-console application, or something else?

    --
    Mats
    Nah in VC++, I choose Win32 console and the code works.. But the code for my application doesn't.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bradszy View Post
    Nah in VC++, I choose Win32 console and the code works.. But the code for my application doesn't.
    Is your application createing a console window with AllocConsole, or what else is different?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    If your problem is only with GetConsoleFontSize() in your other project, make sure you have _WIN32_WINNT defined as 0x0500 (first line of my example). You should put this in your project settings so that it's defined for all source files.

    gg

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. Defining the size of the console...
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2005, 10:35 AM
  4. Increase Font Size Or Bold???
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 08-27-2004, 05:26 PM
  5. Console Size
    By Punkture in forum C Programming
    Replies: 2
    Last Post: 05-08-2003, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21