PowerShellでは、$Host変数によって、ホストアプリケーションへの参照を取得できる。
取得したホストアプリケーションは、System.Management.Automation.Internal.Host.InternalHost という型のインスタンスである。
【 配色 】
コンソールウィンドウの配色を変更するには、以下のプロパティを設定する。
通常の背景色
$Host.UI.RawUI.BackgroundColor
通常の文字色
$Host.UI.RawUI.ForegroundColor
エラーメッセージの背景色
$Host.PrivateData.ErrorBackgroundColor
エラーメッセージの文字色
$Host.PrivateData.ErrorForegroundColor
警告メッセージの背景色
$Host.PrivateData.WarningBackgroundColor
警告メッセージの文字色
$Host.PrivateData.WarningForegroundColor
詳細メッセージの背景色
$Host.PrivateData.VerboseBackgroundColor
詳細メッセージの文字色
$Host.PrivateData.VerboseForegroundColor
デバッグメッセージの背景色
$Host.PrivateData.DebugBackgroundColor
デバッグメッセージの文字色
$Host.PrivateData.DebugForegroundColor
進行状況メッセージの背景色
$Host.PrivateData.ProgressBackgroundColor
進行状況メッセージの文字色
$Host.PrivateData.ProgressForegroundColor
これらのプロパティの型はSystem.ConsoleColor列挙体である。
【 戯れ 】
こんなことができる。
function Hoge
{
$originalColor = $Host.UI.RawUI.BackgroundColor;
for ($index = 0; $index -lt $Args[0].Length; $index++)
{
if ($index % 2 -eq 0)
{
$Host.UI.RawUI.BackgroundColor = [System.ConsoleColor]::Red;
}
else
{
$Host.UI.RawUI.BackgroundColor = [System.ConsoleColor]::Black;
}
$Host.UI.Write($Args[0][$index]);
}
$Host.UI.RawUI.BackgroundColor = $originalColor;
$Host.UI.WriteLine([string]::Empty);
}
Hoge "この文字列だけ背景色が交互に赤と黒になります。";
Hoge "こうすると", "文節単位で", "背景色が", "交互に", "赤と黒に", "なります。";
これの実用性は不明^^;
サンプルコード無意味。。。
よく見たら、$Host.UI.Wite(ConsoleColor, ConsoleColor, string) ってメソッドがあった orz
2006.12.05 19:44 URL | よこけん #- [ 編集 ]
トラックバックURL↓
http://csharper.blog57.fc2.com/tb.php/52-29598634