Changing font in a console application
-
Hi! Is it possible to change the font in a console application? Like iif I have a game where I pick up a megaphone, and want to have a webbding character that happens to look just like a megaphone. Is this possible? thanks Daniel Dan Steeve
Hi Dan, I once did color changes on a Console app, that took some P/Invoke using the following prototypes and definitions:
\[DllImport("kernel32.dll")\] public static extern bool SetConsoleCtrlHandler(ConsoleControlHandler e, bool add); \[DllImport("kernel32.dll")\] public static extern IntPtr GetStdHandle(int nStdHandle); \[DllImport("kernel32.dll")\] public static extern bool SetConsoleTextAttribute(IntPtr windowHandle, int attrib); \[DllImport("KERNEL32.DLL")\] internal static extern int GetConsoleScreenBufferInfo (IntPtr hConsoleOutput, ref CONSOLE\_SCREEN\_BUFFER\_INFO lpConsoleScreenBufferInfo); /// /// Handler to be called when a console event occurs. /// Should return true if no further processing of the event is required. /// public delegate bool ConsoleControlHandler(ConsoleControlEvent consoleEvent); /// /// The event that occurred. /// public enum ConsoleControlEvent { CtrlC=0, CtrlBreak=1, CtrlClose=2, CtrlLogoff=5, CtrlShutdown=6} /// /// Text colors. /// public enum TextColor { // copied from wincon.h, but not used (watch properties of a Console window): // #define FOREGROUND\_BLUE 0x0001 // text color contains blue. // #define FOREGROUND\_GREEN 0x0002 // text color contains green. // #define FOREGROUND\_RED 0x0004 // text color contains red. // #define FOREGROUND\_INTENSITY 0x0008 // text color is intensified. Black=0, DarkBlue=1, DarkGreen=2, // = FOREGROUND\_GREEN Brown=4, DarkMagenta=5, DarkYellow=6, Gray=7, Blue=9, // = FOREGROUND\_BLUE | FOREGROUND\_INTENSITY Green=10, Cyan=11, Red=12, // = FOREGROUND\_RED | FOREGROUND\_INTENSITY Magenta=13, Yellow=14, White=15, Unknown=16 }
This should get you going. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Hi! Is it possible to change the font in a console application? Like iif I have a game where I pick up a megaphone, and want to have a webbding character that happens to look just like a megaphone. Is this possible? thanks Daniel Dan Steeve
As far as I know, the only fonts allowed for Consoles is controlled by the following registry entry. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont Once the font has been added there, a reboot is required. But then, I have no clue how you actually change the font being used, but there's gotta be a Win32 API call somewhere you can p/invoke. I think Luc's examples might help you locate that. Also, in the registry entry, it's not logical because the entry you're likely to see is: 0 REG_SZ Lucinda Console You'd think oh, I just need to add: 1 REG_SZ Consolas Nope, WRONG! You would add 00 REG_SZ Consolas and a third one would be: 000 REG_SZ Courier Yep, real intuitive. :mad:
-
Hi Dan, I once did color changes on a Console app, that took some P/Invoke using the following prototypes and definitions:
\[DllImport("kernel32.dll")\] public static extern bool SetConsoleCtrlHandler(ConsoleControlHandler e, bool add); \[DllImport("kernel32.dll")\] public static extern IntPtr GetStdHandle(int nStdHandle); \[DllImport("kernel32.dll")\] public static extern bool SetConsoleTextAttribute(IntPtr windowHandle, int attrib); \[DllImport("KERNEL32.DLL")\] internal static extern int GetConsoleScreenBufferInfo (IntPtr hConsoleOutput, ref CONSOLE\_SCREEN\_BUFFER\_INFO lpConsoleScreenBufferInfo); /// /// Handler to be called when a console event occurs. /// Should return true if no further processing of the event is required. /// public delegate bool ConsoleControlHandler(ConsoleControlEvent consoleEvent); /// /// The event that occurred. /// public enum ConsoleControlEvent { CtrlC=0, CtrlBreak=1, CtrlClose=2, CtrlLogoff=5, CtrlShutdown=6} /// /// Text colors. /// public enum TextColor { // copied from wincon.h, but not used (watch properties of a Console window): // #define FOREGROUND\_BLUE 0x0001 // text color contains blue. // #define FOREGROUND\_GREEN 0x0002 // text color contains green. // #define FOREGROUND\_RED 0x0004 // text color contains red. // #define FOREGROUND\_INTENSITY 0x0008 // text color is intensified. Black=0, DarkBlue=1, DarkGreen=2, // = FOREGROUND\_GREEN Brown=4, DarkMagenta=5, DarkYellow=6, Gray=7, Blue=9, // = FOREGROUND\_BLUE | FOREGROUND\_INTENSITY Green=10, Cyan=11, Red=12, // = FOREGROUND\_RED | FOREGROUND\_INTENSITY Magenta=13, Yellow=14, White=15, Unknown=16 }
This should get you going. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Pssst, in the 2.0 Framework they added Console.BackgroundColor and Console.ForegroundColor. And there's an enum for ConsoleColor. ;P
Well that is nice to know, thanks. If only they had provided that in time...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google