WindowsフォームにListBox
制御でこれを行うにする唯一の方法は、各列の実際の描画をハイジャックすることによるものです。
まず、ListBox
のDrawMode
プロパティを変更します。
$ListBox.DrawMode = [System.Windows.Forms.DrawMode]::OwnerDrawFixed
これは、私たちはthe DrawItem
event経由アイテムのグラフィックレンダリングを上書きすることができます。
ここで必要なのは、アイテムを描画する関数を定義することだけです。選択した項目に影響を与えることなく、交互の行の色を実行すると、this great example in C#が見つかりました。
幸いにも、C#はeasily ported to PowerShell次のとおりです。
$ListBox.add_DrawItem({
param([object]$s, [System.Windows.Forms.DrawItemEventArgs]$e)
if ($e.Index -gt -1)
{
Write-Host "Drawing item at index $($e.Index)"
<# If the item is selected set the background color to SystemColors.Highlight
or else set the color to either WhiteSmoke or White depending if the item index is even or odd #>
$color = if(($e.State -band [System.Windows.Forms.DrawItemState]::Selected) -eq [System.Windows.Forms.DrawItemState]::Selected){
[System.Drawing.SystemColors]::Highlight
}else{
if($e.Index % 2 -eq 0){
[System.Drawing.Color]::WhiteSmoke
}else{
[System.Drawing.Color]::White
}
}
# Background item brush
$backgroundBrush = New-Object System.Drawing.SolidBrush $color
# Text color brush
$textBrush = New-Object System.Drawing.SolidBrush $e.ForeColor
# Draw the background
$e.Graphics.FillRectangle($backgroundBrush, $e.Bounds)
# Draw the text
$e.Graphics.DrawString($s.Items[$e.Index], $e.Font, $textBrush, $e.Bounds.Left, $e.Bounds.Top, [System.Drawing.StringFormat]::GenericDefault)
# Clean up
$backgroundBrush.Dispose()
$textBrush.Dispose()
}
$e.DrawFocusRectangle()
})
のEt出来上がり:あなたのコードのルックスによって
優秀!正確に私が必要としたもの。 – nkasco
お寄せください。しかし、@boeproxは正しいですが、Windowsフォームを使用するのではなく、WPFとXAMLの使用を検討してください。ずっとメンテナンス可能 –