PowerShell 和 Winforms,试图将组合框的选择颜色更改为 DropDownList 和 OwnerDrawFixed

问题描述 投票:0回答:0

我看过这个:Change the Selection Color of a WinForms ComboBox

还有这个网站上的许多其他帖子,但没有一个对我有用。我认为这可能是我使 C# 代码适应我的应用程序的方式。

表格截图如下:

我正在尝试更改下拉菜单的样式。当您将 ComboBox 切换为 DropDownList 时,ComboBox 的样式会更改为深灰色(请参阅“分离模型”以外的所有下拉菜单)。

我已将其中一个下拉菜单更改为 OwnerDrawFixed,然后我使用 DrawItem 事件处理程序处理文本和背景的绘制。

代码如下:

$COMBO_SeparationModel_DrawItem = {

    param(
        [System.Object]
        $sender, 
        [System.Windows.Forms.DrawItemEventArgs]
        $e
    )

    $Brush = [System.Drawing.SolidBrush]::new([System.Drawing.Color]::Black)  
    $Point = [System.Drawing.Point]::new(2, $e.Index * $e.Bounds.Height + 1) 
    $index = $e.Index -ge 0 ? $e.Index : 0  
    
    $ptx = $e.Bounds.X
    $pty = $e.Bounds.Y
    $pt = [System.Drawing.PointF]::new($ptx,$pty)

    $e.DrawBackground()

    if (($e.State -and [System.Windows.Forms.DrawItemState]::Selected) -eq [System.Windows.Forms.DrawItemState]::Selected){
        $e.Graphics.FillRectangle([System.Drawing.SolidBrush]::new([System.Drawing.Color]::Azure), $e.Bounds)
    }else{
        # $e.Graphics.FillRectangle([System.Drawing.SolidBrush]::new($COMBO_SeperationModel.BackColor), $e.Bounds)  
        $e.Graphics.FillRectangle([System.Drawing.SolidBrush]::new($COMBO_SeperationModel.BackColor), [System.Drawing.Rectangle]::new($Point, $e.Bounds.Size))  
    }      
    
    $e.Graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
    $e.Graphics.DrawString($COMBO_SeperationModel.Items[$index].ToString(), $e.Font, $Brush, $pt, [System.Drawing.StringFormat]::GenericDefault); 
    $e.DrawFocusRectangle()

}

$DemucsForm_Load = {
     # $wshell = New-Object -ComObject Wscript.Shell
     # $wshell.Popup($e.State,0,"Done",0x1)
}

. (Join-Path $PSScriptRoot 'DemucsForm.designer.ps1')

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

$FORM_VSYSDemucsUI.ShowDialog()

但是当我运行这段代码时,一切都是 Azure 而不是只有选定的项目:

另一个问题/副作用是,在我输入自定义 DrawItem 事件处理程序后,论坛上的 Label 组件不再抗锯齿。请参阅标题下的“10 Total Files Selected...”文本。

我觉得我已经尝试了一切,但就是无法让它发挥作用。

有人可以看一下并告诉我我做错了什么吗?

我真的很感激任何帮助,这样我就可以继续实际构建功能。

c# powershell winforms combobox
© www.soinside.com 2019 - 2024. All rights reserved.