コンソールの文字色を変える、文字列の出力位置を設定する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

API関数のSetConsoleTextAttribute()やSetConsoleCursorPosition()を用いることで、コンソールに出力される文字列の色を変えたり、文字列の出力位置を自由に設定することができる。 ここでは、コンソール関連の機能をまとめたユーティリティクラスを紹介する。

ソースコード
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Public NotInheritable Class ConsoleUtil

    <DllImport("kernel32.dll")> _
    Friend Shared Function GetStdHandle( _
        ByVal nStdHandle As Integer) _
        As IntPtr
    End Function

    <DllImport("kernel32.dll")> _
    Friend Shared Function SetConsoleTextAttribute( _
        ByVal hConsoleOutput As IntPtr, _
        ByVal wAttributes As Short) _
        As Boolean
    End Function

    <DllImport("kernel32.dll")> _
    Friend Shared Function SetConsoleCursorPosition( _
        ByVal hConsoleOutput As IntPtr, _
        ByVal dwCursorPosition As COORD) _
        As Boolean
    End Function

    <DllImport("kernel32.dll")> _
    Friend Shared Function GetConsoleScreenBufferInfo( _
        ByVal hConsoleOutput As IntPtr, _
        ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) _
        As Boolean
    End Function

    <StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Friend Structure COORD

        Public X As Short
        Public Y As Short

    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Friend Structure SMALL_RECT

        Public Left As Short
        Public Top As Short
        Public Right As Short
        Public Bottom As Short

    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Friend Structure CONSOLE_SCREEN_BUFFER_INFO

        Public Size As COORD
        Public CursorPosition As COORD
        Public Attributes As Short
        Public Window As SMALL_RECT
        Public MaximumWindowSize As COORD

    End Structure

    Friend Const STD_ERROR_HANDLE As Integer = -12
    Friend Const STD_INPUT_HANDLE As Integer = -10
    Friend Const STD_OUTPUT_HANDLE As Integer = -11

    Friend Const FOREGROUND_BLUE As Integer = &H1
    Friend Const FOREGROUND_GREEN As Integer = &H2
    Friend Const FOREGROUND_RED As Integer = &H4
    Friend Const FOREGROUND_INTENSITY As Integer = &H8

    Friend Const BACKGROUND_BLUE As Integer = &H10
    Friend Const BACKGROUND_GREEN As Integer = &H20
    Friend Const BACKGROUND_RED As Integer = &H40
    Friend Const BACKGROUND_INTENSITY As Integer = &H80

    ''' <summary>
    ''' 表示色
    ''' </summary>
    <Flags()> _
    Public Enum ConsoleTextColor As Short

        ForegroundBlack = 0
        ForegroundDarkBlue = FOREGROUND_BLUE
        ForegroundDarkGreen = FOREGROUND_GREEN
        ForegroundDarkCyan = FOREGROUND_BLUE Or FOREGROUND_GREEN
        ForegroundDarkRed = FOREGROUND_RED
        ForegroundDarkMagenta = FOREGROUND_BLUE Or FOREGROUND_RED
        ForegroundDarkYellow = FOREGROUND_GREEN Or FOREGROUND_RED
        ForegroundLightGray = FOREGROUND_BLUE Or FOREGROUND_GREEN Or FOREGROUND_RED

        ForegroundDarkGray = ForegroundBlack Or FOREGROUND_INTENSITY
        ForegroundLightBlue = ForegroundDarkBlue Or FOREGROUND_INTENSITY
        ForegroundLightGreen = ForegroundDarkGreen Or FOREGROUND_INTENSITY
        ForegroundLightCyan = ForegroundDarkCyan Or FOREGROUND_INTENSITY
        ForegroundLightRed = ForegroundDarkRed Or FOREGROUND_INTENSITY
        ForegroundLightMagenta = ForegroundDarkMagenta Or FOREGROUND_INTENSITY
        ForegroundLightYellow = ForegroundDarkYellow Or FOREGROUND_INTENSITY
        ForegroundWhite = ForegroundLightGray Or FOREGROUND_INTENSITY

        BackgroundBlack = 0
        BackgroundDarkBlue = BACKGROUND_BLUE
        BackgroundDarkGreen = BACKGROUND_GREEN
        BackgroundDarkCyan = BACKGROUND_BLUE Or BACKGROUND_GREEN
        BackgroundDarkRed = BACKGROUND_RED
        BackgroundDarkMagenta = BACKGROUND_BLUE Or BACKGROUND_RED
        BackgroundDarkYellow = BACKGROUND_GREEN Or BACKGROUND_RED
        BackgroundLightGray = BACKGROUND_BLUE Or BACKGROUND_GREEN Or BACKGROUND_RED

        BackgroundDarkGray = BackgroundBlack Or BACKGROUND_INTENSITY
        BackgroundLightBlue = BackgroundDarkBlue Or BACKGROUND_INTENSITY
        BackgroundLightGreen = BackgroundDarkGreen Or BACKGROUND_INTENSITY
        BackgroundLightCyan = BackgroundDarkCyan Or BACKGROUND_INTENSITY
        BackgroundLightRed = BackgroundDarkRed Or BACKGROUND_INTENSITY
        BackgroundLightMagenta = BackgroundDarkMagenta Or BACKGROUND_INTENSITY
        BackgroundLightYellow = BackgroundDarkYellow Or BACKGROUND_INTENSITY
        BackgroundWhite = BackgroundLightGray Or BACKGROUND_INTENSITY

    End Enum

    Private Shared hConsoleOutput As IntPtr

    Shared Sub New()

        hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE)

        SetConsoleTextAttribute(hConsoleOutput, FOREGROUND_INTENSITY)

    End Sub

    ''' <summary>
    ''' 表示色
    ''' </summary>
    Public Shared Property Color() As ConsoleTextColor

        Get

            Dim info As CONSOLE_SCREEN_BUFFER_INFO

            GetConsoleScreenBufferInfo(hConsoleOutput, info)

            Return CType(info.Attributes, ConsoleTextColor)

        End Get

        Set(ByVal Value As ConsoleTextColor)

            SetConsoleTextAttribute(hConsoleOutput, Value)

        End Set

    End Property

    ''' <summary>
    ''' カーソルの位置設定
    ''' </summary>
    Public Shared Sub Locate(ByVal x As Integer, ByVal y As Integer)

        Dim location As New COORD

        location.X = CShort(x)
        location.Y = CShort(y)

        SetConsoleCursorPosition(hConsoleOutput, location)

    End Sub

End Class
使用例
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
Public Class Test

    Public Shared Sub Main()

        ConsoleUtil.Locate(5, 5)

        ConsoleUtil.Color = ConsoleUtil.ConsoleTextColor.ForegroundLightBlue Or _
                            ConsoleUtil.ConsoleTextColor.BackgroundLightCyan

        For i As Integer = 0 To 15

            ConsoleUtil.Locate(i, i)

            Console.Write(i)

        Next

        ConsoleUtil.Color = ConsoleUtil.ConsoleTextColor.ForegroundLightYellow Or _
                            ConsoleUtil.ConsoleTextColor.BackgroundBlack

        Console.WriteLine()

    End Sub

End Class
実行結果
実行結果