文字色や背景色などのコンソールテキスト属性を変更する。
Colorコマンドはコンソール全体の文字色や背景色を変えますが、部分的に変えることはできません。
そこで、部分的に文字色や背景色を変えるColorXコマンドを、OS(Vista)標準搭載のVB.NETで作ります。
ColorX.exe 文字色 [背景色]
文字色や背景色は、数字(0~15)か色の名前(Black~White)で指定します。
vbc ColorX.vb
Public Class Class1
Public Shared Sub Main(ByVal Args() As String)
If Args.Length = 0 OrElse Args.Length > 2 Then
Console.WriteLine("Usage: ColorX ForegroundColor [BackgroundColor]")
For Each ColorName As String In ConsoleColor.GetNames(GetType(ConsoleColor))
Console.WriteLine("{0,2} {1}", [Enum].Format(GetType(ConsoleColor), CType([Enum].Parse(GetType(ConsoleColor), ColorName), ConsoleColor), "d"), ColorName)
Next
Exit Sub
End If
Try
If Args.Length = 1 Then
Console.ForegroundColor = CType([Enum].Parse(GetType(ConsoleColor), Args(0), True), ConsoleColor)
Else
Console.ForegroundColor = CType([Enum].Parse(GetType(ConsoleColor), Args(0), True), ConsoleColor)
Console.BackgroundColor = CType([Enum].Parse(GetType(ConsoleColor), Args(1), True), ConsoleColor)
End If
Catch
Console.Error.WriteLine("Source" & vbTab & vbTab & Err.Source & vbLf & "Number" & vbTab & vbTab & Err.Number & vbLf & "Description" & vbTab & Err.Description & vbLf & "DLL Error" & vbTab & vbTab & Err.LastDLLError)
End Try
End Sub
End Class
使用例、文字列「赤青黄」をその文字色で出します。
@echo off
colorx red
set <NUL /p x=赤
colorx blue
set <NUL /p x=青
colorx yellow
set <NUL /p x=黄
colorx 7
« comm1バッチファイル | トップページ | 文字色や背景色などのコンソールテキスト属性を変更する。(その2) »