改变系统颜色

时间:2016-09-24 02:29:31

标签: winforms powershell colors highlight systemcolors

感谢您抽出宝贵时间帮助我。

我正在使用http://api.somrat.info/notes 来构建GUI,我想覆盖默认的系统颜色。

例如,当控件突出显示(PowerShellTextBox)时,表单会显示系统颜色。我想更改颜色以使用ComboBox。到目前为止,我已经尝试过以下代码,但无济于事:

AliceBlue

1 个答案:

答案 0 :(得分:2)

documentation表示您尝试设置的属性是只读的。

您可以通过调用user32.dll SetSysColors函数来执行此操作:

$signature = @'
[DllImport("user32.dll")]
public static extern bool SetSysColors(
    int cElements, 
    int [] lpaElements,
    uint [] lpaRgbValues);
'@

$type = Add-Type -MemberDefinition $signature `
                 -Name Win32Utils `
                 -Namespace SetSysColors `
                 -PassThru

$color = [Drawing.Color]::AliceBlue
# For RGB color values: 
# $color = [Drawing.Color]::FromArgb(255,255,255) 
$elements = @('13')
$colors = [Drawing.ColorTranslator]::ToWin32($color)

$type::SetSysColors($elements.Length, $elements, $colors)

13元素表示COLOR_HIGHLIGHT,即控件中所选项目的颜色。

运行上面的代码后,结果如下:

组合框

enter image description here

文本框

enter image description here

您可以看到实际文本的颜色已更改,几乎看不到。要更改此设置,只需运行:

$color = [Drawing.Color]::Black
$elements = @('14')
$colors = [Drawing.ColorTranslator]::ToWin32($color)
$type::SetSysColors($elements.Length, $elements, $colors)

其中14代表COLOR_HIGHLIGHTTEXT,这是控件中所选项目文字的颜色。

要详细了解SetSysColors支票PInvoke。另外,转到here以查找更多颜色代码。

我不知道是否可以仅使用WinFormsSetSysColor为PowerShell GUI设置突出显示颜色,但您可以考虑使用{{{} 1}} WPF代替TextBox。这样您就可以使用WinFormsSelectionBrush

SelectionOpacity