public static FontCapabilities GetCurrentFont() { var fontName = "unknown"; var supportsDesiredUnicodeRanges = false; if (OperatingSystem.IsWindows()) { var handle = GetStdHandle(StdHandle.OutputHandle); var info = new CONSOLE_FONT_INFOEX(); info.cbSize = (uint)Marshal.SizeOf(info); GetCurrentConsoleFontEx(handle, false, ref info); var isTrueType = (info.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE; fontName = info.FaceName; var font = new Font(fontName, 10); var ranges = GetUnicodeRangesForFont(font); // 10240, 10495 var min = 0x2801; var max = 0x28FF; var test = CheckIfCharInFont('•', font); int unicodeValue = Convert.ToUInt16('\u2801'); supportsDesiredUnicodeRanges = ranges.Any(x => x.Low >= min && x.High <= min && (x.Low <= max && x.High >= max)); } return(new FontCapabilities { FontName = fontName, SupportsDesiredUnicodeRanges = supportsDesiredUnicodeRanges }); }
private static void PrepareConsole() { IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE); if (hnd != INVALID_HANDLE_VALUE) { CONSOLE_FONT_INFOEX font_infoEx = new CONSOLE_FONT_INFOEX(); font_infoEx.cbSize = (uint)Marshal.SizeOf(font_infoEx); if (GetCurrentConsoleFontEx(hnd, false, ref font_infoEx)) { // Set console font to Lucida Console. font_infoEx.FontFamily = TMPF_TRUETYPE; font_infoEx.FaceName = fontName; SetCurrentConsoleFontEx(hnd, false, ref font_infoEx); } } Console.Title = "OPC XML DA 1.00 Server"; Console.OutputEncoding = Encoding.Unicode; Console.BackgroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.White; Console.SetWindowSize(100, 30); Console.Clear(); }
protected void SetupBuffer(IntPtr buffer, ref CONSOLE_FONT_INFOEX fontInfo, ref CONSOLE_MODE_OUTPUT outLast) { // Console mode GetConsoleModeOut(buffer, out outLast); CONSOLE_MODE_OUTPUT mode = outLast | CONSOLE_MODE_OUTPUT.ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(buffer, (uint)mode); // Font CONSOLE_FONT_INFOEX lastFont = new CONSOLE_FONT_INFOEX(); GetCurrentConsoleFontEx(buffer, false, ref lastFont); if (fontInfo.Equals(default(CONSOLE_FONT_INFOEX))) { fontInfo = lastFont; Info.SetFont(lastFont); } CONSOLE_FONT_INFOEX newFont = new CONSOLE_FONT_INFOEX(); newFont.cbSize = (uint)Marshal.SizeOf(newFont); newFont.FaceName = fontInfo.FaceName; newFont.dwFontSize.X = fontInfo.dwFontSize.X; newFont.dwFontSize.Y = fontInfo.dwFontSize.Y; SetCurrentConsoleFontEx(buffer, false, ref newFont); SetConsoleScreenBufferSize(buffer, Info.size); }
public void InitWindow() { IntPtr hwnd = GetConsoleWindow(); int value = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, value & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX); CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX { nFont = 0, dwFontSize = new COORD(pwidth, pheight), FontFamily = (1 << 4), FontWeight = 400, FaceName = "Consolas" }; cfi.cbSize = (uint)Marshal.SizeOf(cfi); SetCurrentConsoleFontEx(wHnd, false, ref cfi); wwidth = Program.ScreenX; wheight = Program.ScreenY; wwidth1 = Program.ScreenX; wheight1 = Program.ScreenY; Console.SetWindowSize(wwidth, wheight); Console.SetBufferSize(wwidth, wheight); Console.Title = wtitle; Console.CursorVisible = false; //RenderBuffer(buffer); }
public static bool IsOutputConsoleFontTrueType() { CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(GetOutputHandle(), false, ref cfi)) return (cfi.Family & TMPF_TRUETYPE) == TMPF_TRUETYPE; return false; }
public override void Disable() { CONSOLE_FONT_INFOEX lastFont = Info.font; CONSOLE_MODE_OUTPUT lastMode = (CONSOLE_MODE_OUTPUT)Buffer.lastMode; SetupBuffer(Buffer.ptr, ref lastFont, ref lastMode); SetConsoleMode(Buffer.ptr, Buffer.lastMode); }
public static bool IsOutputConsoleFontTrueType() { CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(GetOutputHandle(), false, ref cfi)) { return((cfi.Family & TMPF_TRUETYPE) == TMPF_TRUETYPE); } return(false); }
public override void Enable() { GetStdOut(out IntPtr bufferPtr); CONSOLE_FONT_INFOEX bufferFont = Info.font; CONSOLE_MODE_OUTPUT lastMode = 0; SetupBuffer(bufferPtr, ref bufferFont, ref lastMode); Buffer = new OutputHandlerBufferInfo(bufferPtr, (uint)lastMode); }
public override void Disable() { OutputHandlerBufferInfo bufferInfo = Buffers[0]; CONSOLE_FONT_INFOEX lastFont = Info.font; CONSOLE_MODE_OUTPUT lastMode = 0; SetupBuffer(bufferInfo.ptr, ref lastFont, ref lastMode); SetConsoleActiveScreenBuffer(bufferInfo.ptr); SetConsoleMode(bufferInfo.ptr, bufferInfo.lastMode); Buffers = null; WritingBuffer = -1; }
public GameBuilder SetRenderer <TRenderer, TOutputHandler>() where TRenderer : SurfaceRenderer where TOutputHandler : OutputHandler { RendererType = typeof(TRenderer); OutputHandlerType = typeof(TOutputHandler); outputFont = new CONSOLE_FONT_INFOEX(); GetCurrentConsoleFontEx(GetStdHandle((uint)HANDLE.STD_OUTPUT_HANDLE), false, ref outputFont); FontName = outputFont.FaceName; FontWidth = outputFont.dwFontSize.X; FontHeight = outputFont.dwFontSize.Y; return(this); }
public static void SetupConsole() { Console.WindowHeight = 10; Console.WindowWidth = 30; IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE); if (hnd != INVALID_HANDLE_VALUE) { var fontInfo = new CONSOLE_FONT_INFOEX(); fontInfo.cbSize = (uint)Marshal.SizeOf <CONSOLE_FONT_INFOEX>(); GetCurrentConsoleFontEx(hnd, false, ref fontInfo); fontInfo.dwFontSize.X = 0; fontInfo.dwFontSize.Y = 48; SetCurrentConsoleFontEx(hnd, false, ref fontInfo); } }
public override void Enable() { Buffers = new OutputHandlerBufferInfo[BUFFER_COUNT]; for (int i = 0; i < BUFFER_COUNT; i++) { IntPtr bufferPtr = CreateConsoleScreenBuffer( (uint)(BUFFER_ACCESS_MODE.GENERIC_WRITE | BUFFER_ACCESS_MODE.GENERIC_READ), (uint)(BUFFER_SHARE_MODE.FILE_SHARE_WRITE | BUFFER_SHARE_MODE.FILE_SHARE_READ), new IntPtr(0), 1, new IntPtr(0)); CONSOLE_FONT_INFOEX bufferFont = Info.font; CONSOLE_MODE_OUTPUT lastMode = 0; SetupBuffer(bufferPtr, ref bufferFont, ref lastMode); Buffers[i] = new OutputHandlerBufferInfo(bufferPtr, (uint)lastMode); } WritingBuffer = 1; SetConsoleActiveScreenBuffer(Buffers[0].ptr); }
static extern bool SetCurrentConsoleFontEx(IntPtr consoleOutput, bool maximumWindow, ref CONSOLE_FONT_INFOEX consoleCurrentFontEx);
[DllImport("Kernel32.dll")] private static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
/// <summary> /// Sets console window attributes more conducive to an output status window. /// </summary> /// <param name="title">Console window title</param> /// <param name="backgroundColor">Color of window background</param> /// <param name="textColor">Color of console text</param> /// <returns>false if there is NO associated console window</returns> /// <remarks> /// This consists of not only setting the caller-specified title and window colors, but also /// (1)setting the font to a smaller size (still very readable). /// (2)Enabling select text and copy to clipboard. /// (3)Setting the buffer to a very large size (256x9999) to support a lot of scrolling status text. /// (4)Setting the window size to a slightly larger size (reasonable with the smaller font) to make /// the scrolling status text visible longer before it scrolls off the window. /// </remarks> public static bool SetStdStatusProperties(string title, Color backgroundColor, Color textColor) { IntPtr hConsoleOutput = GetStdHandle(STD_HANDLE.OUTPUT); IntPtr hConsoleInput = GetStdHandle(STD_HANDLE.INPUT); if (hConsoleInput.ToInt32() <= 0) { return(false); //No console window! } bool ok = false; //---------------------- //Get Console Attributes //---------------------- ConsoleInputMode conInMode; ok = GetConsoleMode(hConsoleInput, out conInMode); //We don't need to set anything because users are not writing to the console window //ConsoleOutputMode conOutMode; //ok = GetConsoleMode(hOutput, out conOutMode); CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); ok = GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi); CONSOLE_SCREEN_BUFFER_INFOEX csb = new CONSOLE_SCREEN_BUFFER_INFOEX(); csb.cbSize = Marshal.SizeOf(typeof(CONSOLE_SCREEN_BUFFER_INFOEX)); ok = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csb); //--------------------------------------------- //Tweak the console attributes and update them. //--------------------------------------------- //Add ability to click and select text to copy to the clipboard. conInMode |= ConsoleInputMode.ENABLE_QUICK_EDIT_MODE | ConsoleInputMode.ENABLE_EXTENDED_FLAGS; ok = SetConsoleMode(hConsoleInput, conInMode); //Use small but very readable font to optimize desktop real estate. cfi.FontSize_X = 6; cfi.FontSize_Y = 8; ok = SetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi); //Set console text and background colors. csb.Gray = textColor; csb.Black = backgroundColor; ok = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csb); //There are only 16 color values (e.g. Console only supports 4-bit color). //We set the color indices to match the colors we had set above. System.Console.BackgroundColor = ConsoleColor.Black; System.Console.ForegroundColor = ConsoleColor.Gray; System.Console.Title = title; System.Console.BufferWidth = 256; //Make console buffer huge to hold a lot of text before rolling over. System.Console.BufferHeight = 9999; System.Console.WindowWidth = 80; //Nice console window size. User will have to manually System.Console.WindowHeight = 50; //resize or scroll to see additional status text. System.Console.Clear(); //clears the text buffer but also sets the foreground/background colors. return(true); }
public GameConsole(short width, short height, string title = null, string font = "Consolas", short fontwidth = 8, short fontheight = 8) { Console.Clear(); Width = width; Height = height; Title = title; KeyStates = new KeyState[KEYSTATES]; _consolehandle = CreateFile("CONOUT$", 0x40000000, 0x02, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); if (!_consolehandle.IsInvalid) { _screenbuf = new Plane <CharInfo>(Width, Height); _screenrect = new SmallRect() { Left = 0, Top = 0, Right = Width, Bottom = Height }; _screencoord = new Coord() { X = Width, Y = Height }; var cfi = new CONSOLE_FONT_INFOEX() { FaceName = font, FontWidth = fontwidth, FontHeight = fontheight, FontFamily = 0, //FF_DONTCARE FontWeight = 0x0190, //FW_NORMAL FontIndex = 0 }; SetCurrentConsoleFontEx(_consolehandle.DangerousGetHandle(), false, cfi); } if (width > Console.LargestWindowWidth || height > Console.LargestWindowHeight) { throw new InvalidOperationException($"Unable to create console; maximum width/height are {Console.LargestWindowWidth} x {Console.LargestWindowHeight}"); } Console.WindowWidth = width; Console.WindowHeight = height; Console.CursorVisible = false; //Console.OutputEncoding = Encoding.Unicode; _gamethread = new Thread(() => { if (OnUserCreate()) { var sw = Stopwatch.StartNew(); var cont = true; while (cont) { GetKeyStates(); cont = OnUserUpdate(sw.Elapsed); Paint(); } ; } }); }
extern static bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, [In, Out] ref CONSOLE_FONT_INFOEX lpConsoleCurrentFont);
static extern bool SetCurrentConsoleFontEx( IntPtr ConsoleOutput, bool MaximumWindow, CONSOLE_FONT_INFOEX ConsoleCurrentFontEx );
extern static bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX info);
/// <summary> /// Retrieves extended information about the current console font. /// </summary> /// <param name="hConsoleOutput">A handle to the console screen buffer. The handle must have the GENERIC_READ access right.</param> /// <param name="bMaximumWindow">If this parameter is TRUE, font information is retrieved for the maximum window size. If this parameter is FALSE, font information is retrieved for the current window size.</param> /// <param name="lpConsoleCurrentFontEx">A <see cref="CONSOLE_FONT_INFOEX"/> structure that contains the requested font information.</param> /// <returns>If the function succeeds, returns TRUE, otherwise, retun FALSE.</returns> [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern bool GetCurrentConsoleFontEx([In] IntPtr hConsoleOutput, [In] bool bMaximumWindow, [In, Out] ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
static extern int GetCurrentConsoleFontEx( IntPtr hOut, bool maximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx );
[DllImport("kernel32.dll", SetLastError = true)] static extern bool SetCurrentConsoleFontEx(IntPtr consoleOutput, bool maximumWindow, CONSOLE_FONT_INFOEX consoleCurrentFontEx);
public void SetFont(CONSOLE_FONT_INFOEX font) => this.font = font;
public static extern bool SetCurrentConsoleFontEx( IntPtr hConsoleOutput, [MarshalAs(UnmanagedType.Bool)] bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
public static extern bool SetCurrentConsoleFontEx( SafeFileHandle hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
public static unsafe int Main(string[] args) { // whether we are in debug mode bool debug = false; // handle on standard output IntPtr hnd; // old and new console information structures CONSOLE_FONT_INFOEX oldInfo, newInfo, setInfo; // converted char[] string string str; // argument index int i; // process arguments for (i = 0; i < args.Length; i++) { if (args[i].Length < 1) { Console.Error.WriteLine("*** askufont: Nil argument {0}", i + 1); Console.Error.WriteLine(" Enter askufont /? for help."); return(2); } if (args[i][0] != '/') { break; } if (args[i].Length != 2 || args[i][0] != '/') { Console.Error.WriteLine( "*** askufont: Bad option {0}: {1}", i + 1, args[i]); Console.Error.WriteLine(" Enter askufont /? for help."); return(2); } if (args[i][1] == 'd' || args[i][1] == 'D') { debug = true; } else if (args[i][1] == '?') { Console.Error.WriteLine( " askufont is Copyright 2017 Leon van Dommelen."); Console.Error.WriteLine( " This software is made available under the GNU Public License version 3."); Console.Error.WriteLine( " There is no warrantee of any kind on this free software."); Console.Error.WriteLine( "Usage: askufont [/d] FONTNAME"); Console.Error.WriteLine( "Tries to set a TrueType console font."); Console.Error.WriteLine( "/d: Output debug information."); Console.Error.WriteLine( "FONTNAME: a TrueType font name like \"Lucida Console\" or Consolas."); Console.Error.WriteLine( "Works for Windows Vista and later only; in the output of the VER command,"); Console.Error.WriteLine( "disallow the strings \" 2000 \", \" 2003 \", and \" XP \" using FIND."); Console.Error.WriteLine( "See the askufont.cs source for more details."); Console.Error.WriteLine( "Version: Leon van Dommelen 1."); return(2); } else if (args[i][1] == '.') { return(175); } else if (args[i][1] == '!') { Console.WriteLine("askufont.cs by Leon van Dommelen v1"); return(177); } else { Console.Error.WriteLine( "*** askufont: Bad option {0}: {1}", i + 1, args[i]); Console.Error.WriteLine(" Enter askufont /? for help."); return(2); } } // Check the font argument. if (i > args.Length - 1) { Console.Error.WriteLine("*** askufont: Missing font name."); Console.Error.WriteLine(" Enter askufont /? for help."); return(2); } if (i < args.Length - 1) { Console.Error.WriteLine("*** askufont: Misplaced font name."); Console.Error.WriteLine(" Enter askufont /? for help."); return(2); } if (debug) { Console.Error.WriteLine( "Requested Font: {0}", args[i]); } // get a handle on standard output hnd = GetStdHandle(STD_OUTPUT_HANDLE); if (hnd == INVALID_HANDLE_VALUE) { Console.Error.WriteLine( "*** askufont: Unable to get a handle on standard output!"); return(2); } // First determine the current font information. oldInfo = new CONSOLE_FONT_INFOEX(); oldInfo.cbSize = (uint)Marshal.SizeOf(oldInfo); if (!GetCurrentConsoleFontEx(hnd, false, ref oldInfo)) { Console.Error.WriteLine( "*** askufont: Unable to get console font information!"); return(2); } if (debug) { Console.Error.WriteLine( "Old font info block size: {0}", oldInfo.cbSize); Console.Error.WriteLine("Old font font number: {0}", oldInfo.nFont); Console.Error.WriteLine( "Old font width: {0}", oldInfo.dwFontSize.X); Console.Error.WriteLine( "Old font height: {0}", oldInfo.dwFontSize.Y); Console.Error.WriteLine("Old font family: {0}", oldInfo.FontFamily); Console.Error.WriteLine("Old font weight: {0}", oldInfo.FontWeight); str = new string(oldInfo.FaceName); Console.Error.WriteLine("Old font name: {0}", str); } // Set generic data for the new font, taking some from the old font. newInfo = new CONSOLE_FONT_INFOEX(); newInfo.cbSize = (uint)Marshal.SizeOf(newInfo); newInfo.nFont = 0; newInfo.FontFamily = FONT_FAMILY; IntPtr ptr = new IntPtr(newInfo.FaceName); Marshal.Copy(args[i].ToCharArray(), 0, ptr, args[i].Length); newInfo.dwFontSize = new COORD(oldInfo.dwFontSize.X, oldInfo.dwFontSize.Y); newInfo.FontWeight = oldInfo.FontWeight; if (debug) { Console.Error.WriteLine( "New font info block size: {0}", newInfo.cbSize); Console.Error.WriteLine("New font font number: {0}", newInfo.nFont); Console.Error.WriteLine( "New font width: {0}", newInfo.dwFontSize.X); Console.Error.WriteLine( "New font height: {0}", newInfo.dwFontSize.Y); Console.Error.WriteLine("New font family: {0}", newInfo.FontFamily); Console.Error.WriteLine("New font weight: {0}", newInfo.FontWeight); str = new string(newInfo.FaceName); Console.Error.WriteLine("New font name: {0}", str); } // Try setting the new font. SetCurrentConsoleFontEx(hnd, false, ref newInfo); setInfo = new CONSOLE_FONT_INFOEX(); setInfo.cbSize = (uint)Marshal.SizeOf(setInfo); if (!GetCurrentConsoleFontEx(hnd, false, ref setInfo)) { Console.Error.WriteLine( "*** askufont: Unable to get the new font information!"); return(2); } if (debug) { Console.Error.WriteLine( "Got font info block size: {0}", setInfo.cbSize); Console.Error.WriteLine("Got font font number: {0}", setInfo.nFont); Console.Error.WriteLine( "Got font width: {0}", setInfo.dwFontSize.X); Console.Error.WriteLine( "Got font height: {0}", setInfo.dwFontSize.Y); Console.Error.WriteLine("Got font family: {0}", setInfo.FontFamily); Console.Error.WriteLine("Got font weight: {0}", setInfo.FontWeight); str = new string(setInfo.FaceName); Console.Error.WriteLine("Got font name: {0}", str); } // Check that at least we have a TT font now. if ((setInfo.FontFamily & TMPF_TRUETYPE) != TMPF_TRUETYPE) { Console.Error.WriteLine( "*** askufont: Error setting a TrueType font!"); return(2); } // Done. return(0); }