Powershell:如何更改列表框项目颜色以及在选择时更改它们
Powershell: How to change listbox item colors as well as change them when selected
这是我目前的情况:
foreach($group in $userBGroups.Items) {
if($_.State -eq 'Selected') {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightBlue)
$_.Graphics.FillRectangle($brush, $_.bounds)
} finally {
$brush.Dispose()
}
} elseif($group -eq $lbItem) {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightGreen)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose()
}
}
}
除了您想 select 一个已经是绿色的项目的情况外,这在大多数情况下都有效,看起来它必须 select 短暂地 select 然后它立即恢复为绿色。我不知道是什么导致了这种行为。
我确实知道,如果我将 "or" 语句添加到 'Selected' 逻辑中以查找 NoAccelerator,我最终会将所有行着色为浅蓝色,然后当我单击它们时,它们会突出显示一个更深的蓝色或浅绿色,如果它们在没有 NoAccelerator 的情况下会是那种颜色。
所以我设法找到了解决问题的办法。我将 DrawItem 事件修改为如下所示:
$userBGroups_DrawItem=[System.Windows.Forms.DrawItemEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DrawItemEventArgs]
if($userBGroups.Items.Count -eq 0) {return}
$_.DrawBackground()
$lbItem = $userBGroups.Items[$_.Index]
$itemBGColor = [System.Drawing.Color]::White
if($userBGroups.SelectedItems.Contains($lbItem)) {
$itemBGColor = [System.Drawing.Color]::LightBlue
} else {
if($userBGroups.Items -contains $lbItem) {
$itemBGColor = [System.Drawing.Color]::LightGreen
} else {
$itemBGColor = [System.Drawing.Color]::White
}
}
try {
$brush = New-Object System.Drawing.SolidBrush($itemBGColor)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose
}
$_.Graphics.DrawString($lbItem, $_.Font, [System.Drawing.SystemBrushes]::ControlText, (New-Object System.Drawing.PointF($_.Bounds.X, $_.Bounds.Y)))
}
关键是这段代码有效但遗漏了一些东西。每当我选择一个项目然后选择另一个项目时,我仍然会保留之前的选择。为了解决这个问题,我做了以下选择更改事件:
$userBGroups_SelectedValueChanged={
$userBGroups.Refresh()
}
它会导致在重绘时选择项目后发生闪烁,但在我弄清楚是否有解决方法之前是可行的。
这是我目前的情况:
foreach($group in $userBGroups.Items) {
if($_.State -eq 'Selected') {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightBlue)
$_.Graphics.FillRectangle($brush, $_.bounds)
} finally {
$brush.Dispose()
}
} elseif($group -eq $lbItem) {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightGreen)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose()
}
}
}
除了您想 select 一个已经是绿色的项目的情况外,这在大多数情况下都有效,看起来它必须 select 短暂地 select 然后它立即恢复为绿色。我不知道是什么导致了这种行为。
我确实知道,如果我将 "or" 语句添加到 'Selected' 逻辑中以查找 NoAccelerator,我最终会将所有行着色为浅蓝色,然后当我单击它们时,它们会突出显示一个更深的蓝色或浅绿色,如果它们在没有 NoAccelerator 的情况下会是那种颜色。
所以我设法找到了解决问题的办法。我将 DrawItem 事件修改为如下所示:
$userBGroups_DrawItem=[System.Windows.Forms.DrawItemEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DrawItemEventArgs]
if($userBGroups.Items.Count -eq 0) {return}
$_.DrawBackground()
$lbItem = $userBGroups.Items[$_.Index]
$itemBGColor = [System.Drawing.Color]::White
if($userBGroups.SelectedItems.Contains($lbItem)) {
$itemBGColor = [System.Drawing.Color]::LightBlue
} else {
if($userBGroups.Items -contains $lbItem) {
$itemBGColor = [System.Drawing.Color]::LightGreen
} else {
$itemBGColor = [System.Drawing.Color]::White
}
}
try {
$brush = New-Object System.Drawing.SolidBrush($itemBGColor)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose
}
$_.Graphics.DrawString($lbItem, $_.Font, [System.Drawing.SystemBrushes]::ControlText, (New-Object System.Drawing.PointF($_.Bounds.X, $_.Bounds.Y)))
}
关键是这段代码有效但遗漏了一些东西。每当我选择一个项目然后选择另一个项目时,我仍然会保留之前的选择。为了解决这个问题,我做了以下选择更改事件:
$userBGroups_SelectedValueChanged={
$userBGroups.Refresh()
}
它会导致在重绘时选择项目后发生闪烁,但在我弄清楚是否有解决方法之前是可行的。