Skip to content

Instantly share code, notes, and snippets.



Last active Jan 3, 2021
What would you like to do?
using System;
using System.Runtime.InteropServices;
namespace TestCsharpConsole
public static class ConsoleHelper
public static void Write(int left, int top, string text, ConsoleColor color = ConsoleColor.White)
Console.ForegroundColor = color;
Console.SetCursorPosition(left, top);
public static void DisableAllResizingControl()
var window = GetConsoleWindow();
var systemMenu = GetSystemMenu(window, false);
DeleteMenu(systemMenu, ScClose, MfByCommand);
DeleteMenu(systemMenu, ScMinimize, MfByCommand);
DeleteMenu(systemMenu, ScMaximize, MfByCommand);
DeleteMenu(systemMenu, ScSize, MfByCommand);
public static void SetConsoleFont(string fontName, short size)
var hnd = GetStdHandle(StdHandle.OutputHandle);
if (hnd != InvalidHandleValue)
var info = new CONSOLE_FONT_INFO_EX();
info.cbSize = (uint)Marshal.SizeOf(info);
// Set console font to Lucida Console.
var newInfo = new CONSOLE_FONT_INFO_EX();
newInfo.cbSize = (uint)Marshal.SizeOf(newInfo);
newInfo.FontFamily = TmpfTrueType;
IntPtr ptr = new IntPtr(newInfo.FaceName);
Marshal.Copy(fontName.ToCharArray(), 0, ptr, fontName.Length);
// Get some settings from current font.
newInfo.dwFontSize = new COORD(info.dwFontSize.X, size);
SetCurrentConsoleFontEx(hnd, false, ref newInfo);
// Don't mind the code below
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private unsafe struct CONSOLE_FONT_INFO_EX
internal uint cbSize;
internal int FontIndex;
internal COORD dwFontSize;
internal int FontFamily;
internal int FontWeight;
internal fixed char FaceName[LfFaceSize];
private struct COORD
internal short X;
internal short Y;
internal COORD(short x, short y)
X = x;
Y = y;
private enum StdHandle
OutputHandle = -11
private const int TmpfTrueType = 4;
private const int LfFaceSize = 32;
private const int MfByCommand = 0x0;
private const int ScClose = 0xF060;
private const int ScMinimize = 0xF020;
private const int ScMaximize = 0xF030;
private const int ScSize = 0xF000;
private static IntPtr InvalidHandleValue = new IntPtr(-1);
private static extern IntPtr GetStdHandle(StdHandle index);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
private static extern IntPtr GetSystemMenu(IntPtr handle, bool revert);
private static extern IntPtr DeleteMenu(IntPtr handle, int position, int flags);
[DllImport("kernel32", SetLastError = true)]
private static extern bool SetCurrentConsoleFontEx(
IntPtr consoleOutput,
bool maximumWindow,
ref CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment