Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
SetCurrentConsoleFontEx (kernel32)
 
.
Summary
This function allows to set the font dimensions on consoles and more.

C# Signature:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern Int32 SetCurrentConsoleFontEx(
        IntPtr ConsoleOutput,
        bool MaximumWindow,
        ref CONSOLE_FONT_INFO_EX ConsoleCurrentFontEx);

VB Signature:

<DllImport("kernel32.dll")>
    Public Shared Function SetCurrentConsoleFontEx(ConsoleOutput As IntPtr, MaximumWindow As Boolean, ConsoleCurrentFontEx As CONSOLE_FONT_INFO_EX) As Boolean
    End Function

User-Defined Types:

C#:

    [StructLayout(LayoutKind.Sequential)]
    public struct COORD
    {
        public short X;
        public short Y;

        public COORD(short X, short Y)
        {
        this.X = X;
        this.Y = Y;
        }
    };

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct CONSOLE_FONT_INFO_EX
    {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public int FontFamily;
        public int FontWeight;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] // Edit sizeconst if the font name is too big
        public string FaceName;
    }

You must set cbSize with Marshal.SizeOf using the instance of CONSOLE_FONT_INFO_EX.

Vb:

<StructLayout(LayoutKind.Sequential)>
    Public Structure COORD
    Public X As Short
    Public Y As Short

    Public Sub New(X As Short, Y As Short)
        Me.X = X
        Me.Y = Y
    End Sub
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure CONSOLE_FONT_INFO_EX
    Public cbSize As UInteger
    Public nFont As UInteger
    Public dwFontSize As COORD
    Public FontFamily As UShort
    Public FontWeight As UShort

    Public face0, face1, face2, face3, face4, face5, face6, face7 As UInt64
    End Structure

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

// Untested, added during 1-pass copy & paste of windows console fuctions

// Update: C# has been tested

When on Windows XP, kernel32 does NOT support SetCurrentConsoleFontEx and cannot be used. It will throw an error saying entry point not found because of this.

Minimum supported client: Windows Vista

Tips & Tricks:

Please add some!

Sample Code:

C#:

using System.Runtime.InteropServices;
using System;

namespace Win32test
{
    class ConsoleTest
    {

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern Int32 SetCurrentConsoleFontEx(
        IntPtr ConsoleOutput,
        bool MaximumWindow,
        ref CONSOLE_FONT_INFO_EX ConsoleCurrentFontEx);

    private enum StdHandle
    {
        OutputHandle = -11
    }

    [DllImport("kernel32")]
    private static extern IntPtr GetStdHandle(StdHandle index);

    private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

    static void Main(string[] args)
    {
        // Setting the font and fontsize
        // Other values can be changed too

        Console.Write("Before. Press any key to continue");
        Console.ReadKey();

        // Instantiating CONSOLE_FONT_INFO_EX and setting its size (the function will fail otherwise)
        CONSOLE_FONT_INFO_EX ConsoleFontInfo = new CONSOLE_FONT_INFO_EX();
        ConsoleFontInfo.cbSize = (uint)Marshal.SizeOf(ConsoleFontInfo);

        // Optional, implementing this will keep the fontweight and fontsize from changing
        // See notes
        // GetCurrentConsoleFontEx(GetStdHandle(StdHandle.OutputHandle), false, ref ConsoleFontInfo);

        ConsoleFontInfo.FaceName = "Lucida Console";
        ConsoleFontInfo.dwFontSize.X = 6;
        ConsoleFontInfo.dwFontSize.Y = 8;

        SetCurrentConsoleFontEx(GetStdHandle(StdHandle.OutputHandle), false, ref ConsoleFontInfo);

        Console.Clear();
        Console.Write("After. Notice how the font type and size changed?");
        Console.ReadKey();
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct COORD
    {
        public short X;
        public short Y;

        public COORD(short X, short Y)
        {
        this.X = X;
        this.Y = Y;
        }
    };

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct CONSOLE_FONT_INFO_EX
    {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public int FontFamily;
        public int FontWeight;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] // Edit sizeconst if the font name is too big
        public string FaceName;
    }
    }
}

Notes:

Check out GetCurrentConsoleFontEx

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions