Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Changing font in a console application

Changing font in a console application

Scheduled Pinned Locked Moved C#
game-devquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DanSteeve
    wrote on last edited by
    #1

    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

    L C 2 Replies Last reply
    0
    • D DanSteeve

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      C 1 Reply Last reply
      0
      • D DanSteeve

        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

        C Offline
        C Offline
        ChrisKo 0
        wrote on last edited by
        #3

        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:

        1 Reply Last reply
        0
        • L Luc Pattyn

          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


          C Offline
          C Offline
          ChrisKo 0
          wrote on last edited by
          #4

          Pssst, in the 2.0 Framework they added Console.BackgroundColor and Console.ForegroundColor. And there's an enum for ConsoleColor. ;P

          L 1 Reply Last reply
          0
          • C ChrisKo 0

            Pssst, in the 2.0 Framework they added Console.BackgroundColor and Console.ForegroundColor. And there's an enum for ConsoleColor. ;P

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups