Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 更改系统颜色_Winforms_Powershell_Colors_Highlight_Systemcolors - Fatal编程技术网

Winforms 更改系统颜色

Winforms 更改系统颜色,winforms,powershell,colors,highlight,systemcolors,Winforms,Powershell,Colors,Highlight,Systemcolors,谢谢你抽出时间来帮助我 我正在使用PowerShell构建GUI,我希望覆盖默认的系统颜色 例如,当控件高亮显示(TextBox或ComboBox)时,表单显示系统颜色。我想将颜色更改为使用AliceBlue。到目前为止,我已经尝试了以下代码,但没有效果: [System.Drawing.SystemColors]::Highlight = 'AliceBlue' [System.Drawing.SystemColors]::HighlightText = 'AliceBlue' [System

谢谢你抽出时间来帮助我

我正在使用
PowerShell
构建GUI,我希望覆盖默认的系统颜色

例如,当控件高亮显示(
TextBox
ComboBox
)时,表单显示系统颜色。我想将颜色更改为使用
AliceBlue
。到目前为止,我已经尝试了以下代码,但没有效果:

[System.Drawing.SystemColors]::Highlight = 'AliceBlue'
[System.Drawing.SystemColors]::HighlightText = 'AliceBlue'
[System.Drawing.SystemColors]::ScrollBar = 'AliceBlue'
[System.Drawing.SystemColors]::Control = 'AliceBlue'
[System.Drawing.SystemColors]::HotTrack = 'AliceBlue'
[System.Drawing.SystemColors]::Window = 'AliceBlue'
[System.Drawing.SystemColors]::WindowFrame = 'AliceBlue'
说明您尝试设置的属性是只读的

您可以通过调用
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\u HIGHLIGHT
,它是控件中所选项目的颜色


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

组合框

文本框


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

$color = [Drawing.Color]::Black
$elements = @('14')
$colors = [Drawing.ColorTranslator]::ToWin32($color)
$type::SetSysColors($elements.Length, $elements, $colors)
其中
14
表示
COLOR\u HIGHLIGHTTEXT
,它是控件中所选项目文本的颜色

要查看有关
SetSysColors
的更多信息,请选中。另外,请查找更多颜色代码


< P> >我不知道是否可以为PultS壳GUI设置高亮颜色,而不使用<代码> WiFrase>代码>或<代码> SETIsSCORE < /代码>,但您可以考虑的一种方式是使用<代码> WPF <代码>文本框< /代码>,而不是<代码> WiFrase。这样您就可以使用
SelectionBrush
selectionapacity

[xml]$xaml = @"
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" 
       Title="Initial Window" 
       WindowStartupLocation = "CenterScreen" 
       ResizeMode="NoResize"
       SizeToContent = "WidthAndHeight" 
       ShowInTaskbar = "True" 
       Background = "lightgray"> 
    <StackPanel >  
        <Label Content='Type in this textbox' />
        <TextBox x:Name="InputBox" 
                    Height = "50" 
                    SelectionBrush= "Green"
                    SelectionOpacity = "0.5" />  
    </StackPanel>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )


$Window.ShowDialog() | Out-Null
[xml]$xaml=@”
"@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Window=[Windows.Markup.XamlReader]::加载($reader)
$Window.ShowDialog()| Out Null

亲爱的Bassie,非常感谢您在短时间内提供的所有帮助。这周你休息得很好!亲爱的Bassie,运行上述代码时出现问题。代码更改了所需的颜色,但该代码对整个操作系统有效。例如,当我打开记事本并突出显示文本时,我可以看到AliceBlue颜色。有没有办法限制Winform的颜色?感谢You@NickPrice你好,尼克,我在回答中添加了另一个可能的解决方案,但它要求您使用
WPF
TextBox
。希望这对Dar Bassie有所帮助,我尝试了您的WPF代码,但在$Window.ShowDialog()处出现了错误“您不能对空值表达式调用方法”。顺便问一下,有没有办法通过使用WPF代码覆盖系统颜色,我可以在PowerShell表单中使用这些代码?再次感谢您的宝贵意见。-谢谢,基本上它说“错误:找不到类型[Windows.Markup.XamlReader]:请确保已加载包含此类型的程序集。”