Skip to content

Instantly share code, notes, and snippets.

@mr5z
Last active January 3, 2021 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/accb3847e9eca545cfc13da196f05e91 to your computer and use it in GitHub Desktop.
Save mr5z/accb3847e9eca545cfc13da196f05e91 to your computer and use it in GitHub Desktop.
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);
Console.Write(text);
}
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)
{
unsafe
{
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];
}
[StructLayout(LayoutKind.Sequential)]
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);
[DllImport("kernel32")]
private static extern IntPtr GetStdHandle(StdHandle index);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32")]
private static extern IntPtr GetSystemMenu(IntPtr handle, bool revert);
[DllImport("user32")]
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