■ 콘솔 폰트를 설정하는 방법을 보여준다.
▶ COORD.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 좌표 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct COORD { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// X /// </summary> public short X; /// <summary> /// Y /// </summary> public short Y; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - COORD(x, y) /// <summary> /// 생성자 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> public COORD(short x, short y) { X = x; Y = y; } #endregion } } |
▶ CONSOLE_FONT_INFO_EX.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 콘솔 폰트 정보 확장 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public unsafe struct CONSOLE_FONT_INFO_EX { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public uint Size; /// <summary> /// 폰트 /// </summary> public uint Font; /// <summary> /// 폰트 크기 /// </summary> public COORD FontSize; /// <summary> /// 폰트 패밀리 /// </summary> public int FontFamily; /// <summary> /// 폰트 가중치 /// </summary> public int FontWeight; /// <summary> /// 페이스명 /// </summary> public fixed char FaceName[ConsoleHelper.LF_FACESIZE]; #endregion } } |
▶ ConsoleHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 콘솔 헬퍼 /// </summary> public static class ConsoleHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 표준 핸들 구하기 - GetStdHandle(type) /// <summary> /// 표준 핸들 구하기 /// </summary> /// <param name="type">타입</param> /// <returns>표준 핸들</returns> [DllImport("kernel32", SetLastError = true)] private static extern IntPtr GetStdHandle(int type); #endregion #region 현재 콘솔 폰트 설정하기 - SetCurrentConsoleFontEx(outputHandle, maximumWindow, info) /// <summary> /// 현재 콘솔 폰트 설정하기 /// </summary> /// <param name="outputHandle">출력 핸들</param> /// <param name="maximumWindow">최대 창 크기 글꼴 정보 여부</param> /// <param name="info">콘솔 폰트 정보 확장</param> /// <returns>처리 결과</returns> [DllImport("kernel32", SetLastError = true)] private static extern bool SetCurrentConsoleFontEx(IntPtr outputHandle, bool maximumWindow, ref CONSOLE_FONT_INFO_EX info); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// INVALID_HANDLE_VALUE /// </summary> private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// LF_FACESIZE /// </summary> public const int LF_FACESIZE = 32; #endregion //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// STD_OUTPUT_HANDLE /// </summary> private const int STD_OUTPUT_HANDLE = -11; /// <summary> /// TMPF_TRUETYPE /// </summary> private const int TMPF_TRUETYPE = 4; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 폰트 설정하기 - SetFont(fontName, fontWidth, fontheight) /// <summary> /// 폰트 설정하기 /// </summary> /// <param name="fontName">폰트명</param> /// <param name="fontWidth">폰트 너비</param> /// <param name="fontheight">폰트 높이</param> public static void SetFont(string fontName, short fontWidth, short fontheight) { unsafe { IntPtr windowHandle = GetStdHandle(STD_OUTPUT_HANDLE); if(windowHandle != INVALID_HANDLE_VALUE) { CONSOLE_FONT_INFO_EX info = new CONSOLE_FONT_INFO_EX(); info.Size = (uint)Marshal.SizeOf(info); info.FontSize = new COORD(fontWidth, fontheight); info.FontFamily = TMPF_TRUETYPE; IntPtr pointer = new IntPtr(info.FaceName); Marshal.Copy(fontName.ToCharArray(), 0, pointer, fontName.Length); SetCurrentConsoleFontEx(windowHandle, false, ref info); } } } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ConsoleHelper.SetFont("궁서체", 8, 16); Console.WriteLine("가나다라마바사아자차카타파하1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"); Console.ReadKey(false); } #endregion } } |