【问题标题】:Set each items BackColor property of a ComboBox设置 ComboBox 的每个项目的 BackColor 属性
【发布时间】:2021-05-13 19:53:05
【问题描述】:

这是我目前在下面的代码,其中填充了数组([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.Font            = [System.Drawing.Font]::New("Segoe UI", 10, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$Form.StartPosition   = 'CenterScreen'
$Form.Text            = 'How do I Change Each Items Colour to Match Their Name?'
$Form.Width           = 500
$Form.Height          = 69
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle # 1 A fixed, single-line border
$Form.SizeGripStyle   = [System.Windows.Forms.SizeGripStyle]::Hide # 2 The sizing grip is hidden.

$combo                    = New-Object System.Windows.Forms.ComboBox
$combo.DrawMode           = 'OwnerDrawFixed'
$combo.FlatStyle          = [System.Windows.Forms.FlatStyle]::System
$combo.Font               = [System.Drawing.Font]::New("Segoe UI", 14, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$combo.Width              = $Form.Width-1-$combo.Height+($combo.Margin.Left+$combo.Margin.Right)
$combo.AutoCompleteMode   = 'SuggestAppend' # Enables searching but not mid string. Need to find a solution for this.
$combo.AutoCompleteSource = 'ListItems'
$combo.AutoSize           = $true
$combo.DropDownStyle      = 'DropDownList'
$combo.ItemHeight         = 24

$colorNamesArr = ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name # Here's the array of colour that are to get added to the ComboBox.
$combo.Items.AddRange($colorNamesArr) # Add the array to the ComboBox.
$Form.Controls.Add($combo)

# HERE'S WHERE I NEED HELP.

# How do I change the colours of the ComboBox items to match that of their name.

$combo.Add_DrawItem({param($sender,$e)
    $text = "-- Select Color --"
    if ($e.Index -gt -1){
        $text = $sender.GetItemText($sender.Items[$e.Index])
    }
    $e.DrawBackground()

    [System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $text, $combo.Font, $e.Bounds, $e.ForeColor, [System.Windows.Forms.TextFormatFlags]::Default)

})
$Form.ShowDialog() | Out-Null
$Form.Dispose()

【问题讨论】:

    标签: winforms powershell combobox


    【解决方案1】:

    您需要覆盖绘制 ComboBox 的事件。如果您想在每个名称旁边添加颜色,下面是一个示例,说明您可以如何做到这一点。

    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()
    

    调整设置以将绘图更改为您的偏好。

    this code改编为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 覆盖对象框内的定位。高度和宽度是盒子本身的大小。

    【讨论】:

    • 酷,谢谢。当您将鼠标悬停在项目上时,您知道是什么导致项目名称变得有点模糊和粗体吗?见这里:i.imgur.com/8vKZufm.png
    • 我认为当您将鼠标悬停在文本上时正在重新绘制文本,使其看起来有点模糊。
    • 不用担心。当您下拉 ComboBox 时,这不会突出显示项目,这也是第一个所做的。我在$Rectangle = $e.Bounds 之后添加了$e.DrawBackground(),这就解决了这个问题。因此,如果您想将其添加到答案中,我会接受它,那就太好了。我仍然不确定如何将样本移到左侧,并将其旁边的文本移到右侧,但我现在正在研究。
    • 请参阅更新后的答案,其中包含如何将颜色移到左侧的示例。
    • 非常感谢@Ash。
    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多