设置 ComboBox 的每个项目 BackColor 属性
Set each items BackColor property of a ComboBox
这是我到目前为止的代码,其中填充了一个数组 ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name
。
我试图查看是否有任何方法可以设置每个项目的 BackColor
属性 以匹配相同颜色数组元素的 none。
理想情况下,我想要这样的东西,在名称的左侧有一些颜色样本。
编辑:
从那以后,我发现您可以为控件添加一个事件处理程序,但我仍然不确定如何更改每个项目以显示一个小样本。我已经更新了下面的代码,因为其他代码不是那么接近。
我目前的代码:
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()
您需要覆盖绘制组合框的事件。如果您想在每个名称旁边添加一种颜色,这里是您可以如何操作的示例。
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 涵盖对象在框内的定位。高度和宽度是盒子本身的尺寸。
这是我到目前为止的代码,其中填充了一个数组 ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name
。
我试图查看是否有任何方法可以设置每个项目的 BackColor
属性 以匹配相同颜色数组元素的 none。
理想情况下,我想要这样的东西,在名称的左侧有一些颜色样本。
编辑: 从那以后,我发现您可以为控件添加一个事件处理程序,但我仍然不确定如何更改每个项目以显示一个小样本。我已经更新了下面的代码,因为其他代码不是那么接近。
我目前的代码:
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()
您需要覆盖绘制组合框的事件。如果您想在每个名称旁边添加一种颜色,这里是您可以如何操作的示例。
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 涵盖对象在框内的定位。高度和宽度是盒子本身的尺寸。