Winforms 设置组合框的每个项目背景色属性

Winforms 设置组合框的每个项目背景色属性,winforms,powershell,combobox,Winforms,Powershell,Combobox,下面是我目前掌握的代码,其中填充了一个数组([System.Drawing.Color]| Get Member-Static-MemberType Properties)。name 我试图看看是否有任何方法可以将每个项目的BackColor属性设置为与相同颜色数组元素的属性匹配,但没有 理想情况下,我想得到这样的东西,名字左边有一个小的颜色样本 编辑: 我发现可以为控件添加事件处理程序,但我仍然不确定如何更改每个项目以显示一个小样例。我已经更新了下面的代码,因为另一个代码没有那么接近 到目前

下面是我目前掌握的代码,其中填充了一个数组
([System.Drawing.Color]| Get Member-Static-MemberType Properties)。name

我试图看看是否有任何方法可以将每个项目的
BackColor
属性设置为与相同颜色数组元素的属性匹配,但没有

理想情况下,我想得到这样的东西,名字左边有一个小的颜色样本

编辑: 我发现可以为控件添加事件处理程序,但我仍然不确定如何更改每个项目以显示一个小样例。我已经更新了下面的代码,因为另一个代码没有那么接近

到目前为止,我掌握的代码是:
您需要覆盖绘制组合框的事件。如果您想在每个名称旁边添加颜色,下面是一个示例

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form               = New-Object system.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$Form.Text          = "How do I Change Each Items Colour to Match Their Name?"
$Form.Width         = 500
$Form.Height        = 72

$cbSearch               = New-Object system.Windows.Forms.ComboBox
$cbSearch.Font          = [System.Drawing.Font]::New("Segoe UI", 14, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$cbSearch.Width         = $Form.Width-1-$cbSearch.Height+($cbSearch.Margin.Left+$cbSearch.Margin.Right)
$cbSearch.DropDownStyle = 'DropDownList'

# Setting up the ComboBox.
$colorNamesArr = ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name
#Add the array to the ComboBox.
$colorNamesArr | ForEach-Object {[void] $cbSearch.Items.Add($_)}
#Set the first item when opened.
$cbSearch.Text = $colorNamesArr[0]

## Allow override of graphical rendering of the Combobox.
$cbSearch.DrawMode = [System.Windows.Forms.DrawMode]::OwnerDrawFixed

## Add an event that draws each item.
$cbSearch.add_DrawItem({
    param([object]$s, [System.Windows.Forms.DrawItemEventArgs]$e)

    $Graphics = $e.Graphics
    $Rectangle = $e.Bounds
    # Clear the previous drawing
    $e.DrawBackground()
    if ($e.Index -ge 0) {
        $Name = ([System.Windows.Forms.ComboBox]$s).Items[$e.Index].ToString()
        $Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Regular)
        $Color = [System.Drawing.Color]::FromName($Name)
        $Brush = [System.Drawing.SolidBrush]::new($Color)
        # Can uncomment this if you do not want the background highlighted when hovering over item.
        # $Graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
        $Graphics.DrawString($Name, $Font, [System.Drawing.Brushes]::Black, $Rectangle.X, $Rectangle.Top)
        $Graphics.FillRectangle($Brush, $Rectangle.X + 110, $Rectangle.Y + 5, $Rectangle.Width -10, $Rectangle.Height -10)

        # Dispose of disposable objects
        $Brush.Dispose()
    }
})

# Add any controls to the form.
$Form.Controls.Add($cbSearch)

# This below needs to be added to focus the dialog when it opens after the ColorDialog.
$Form.Add_Shown({$Form.Activate(); $cbSearch.Focus()})

[void]$Form.ShowDialog()
使用设置将图形更改为您的首选项

从中改编为PowerShell


编辑

如果要更改彩色框的位置和大小,请参见此示例

$Graphics.DrawString($Name, $Font, [System.Drawing.Brushes]::Black, $Rectangle.X + 40, $Rectangle.Top + 5)
$Graphics.FillRectangle($Brush, $Rectangle.X + 10, $Rectangle.Y + 2, $Rectangle.Width - 440, $Rectangle.Height - 4)

X和Y覆盖对象框内的定位。高度和宽度是盒子本身的大小。

很酷,谢谢。您知道是什么原因导致您将鼠标悬停在项目上时项目名称变得模糊和粗体吗?请看这里:我认为当你在上面悬停时,文本正在被重新绘制,看起来有点模糊。不用担心。当您下拉组合框时,它不会突出显示项目,这也是第一个组合框所做的。我在
$Rectangle=$e.Bounds
之后添加了
$e.trackground()
,这就解决了问题。所以如果你想在答案中加上这个,我会接受的,那就太好了。我仍然不确定如何将样本移到左边,旁边的文本移到右边,但我现在正在研究。请查看更新后的答案,以及如何将颜色移到左边的示例。非常感谢@Ash。
$Graphics.DrawString($Name, $Font, [System.Drawing.Brushes]::Black, $Rectangle.X + 40, $Rectangle.Top + 5)
$Graphics.FillRectangle($Brush, $Rectangle.X + 10, $Rectangle.Y + 2, $Rectangle.Width - 440, $Rectangle.Height - 4)