我正在研究WPF应用程序,我正在使用Telerik对话框控件,它显示为Window,但实际上并不是Window对象。
因为它不是一个真正的窗口(但它看起来像一个窗口),它的样式与我的应用程序中使用的其余对话框不匹配。
所以,我已经为这个对话框创建了一个模板,我正在为系统颜色分配系统颜色,以便对话框使用相同的颜色,并且具有与普通窗口相同的外观和感觉。
由于某种原因,我找不到“窗口颜色”的系统颜色(不是窗口背景颜色,那个很容易找到......我说的是实际的窗口颜色......框架和标题窗口)
我已经浏览了所有的System.Windows.SystemColors但找不到定义“窗口颜色”的系统颜色......就像我说的,我不想引用“窗口的背景颜色” ”
我想知道是否有人知道如何检索这种颜色。
修改 以下是我在尝试确定哪种系统颜色用于Window Color时实现的一些代码。
以下WPF代码是一组边框,其背景设置为每个系统颜色可用...具有系统颜色名称的TextBlock位于每个边框内。
如果您想亲自检查一下,请将以下内容放入Window或其他内容中,以便您可以看到它:
<ScrollViewer>
<StackPanel>
<-- here I had another Stack Panel referencing the Brushes
instead of using the Keys. I wanted the application to be dynamic
so that if the user changed colors it would be reflected in
the application on the fly...-->
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="White" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="4" Color="Black" ShadowDepth="0"/>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Margin" Value="2"/>
<Setter Property="Height" Value="20"/>
</Style>
</StackPanel.Resources>
<Border>
<Border.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.WindowColorKey}}"/>
</Border.Background>
<TextBlock Text="SystemColors.WindowColorKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" >
<TextBlock Text="SystemColors.ActiveBorderBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" >
<TextBlock Text="SystemColors.ActiveCaptionBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" >
<TextBlock Text="SystemColors.ActiveCaptionTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.AppWorkspaceBrushKey}}" >
<TextBlock Text="SystemColors.AppWorkspaceBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" >
<TextBlock Text="SystemColors.ControlBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" >
<TextBlock Text="SystemColors.ControlDarkBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" >
<TextBlock Text="SystemColors.ControlDarkDarkBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" >
<TextBlock Text="SystemColors.ControlLightBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}" >
<TextBlock Text="SystemColors.ControlLightLightBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" >
<TextBlock Text="SystemColors.ControlTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}" >
<TextBlock Text="SystemColors.DesktopBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" >
<TextBlock Text="SystemColors.GradientActiveCaptionBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.GradientInactiveCaptionBrushKey}}" >
<TextBlock Text="SystemColors.GradientInactiveCaptionBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" >
<TextBlock Text="SystemColors.GrayTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" >
<TextBlock Text="SystemColors.HighlightBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" >
<TextBlock Text="SystemColors.HighlightTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" >
<TextBlock Text="SystemColors.HotTrackBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}" >
<TextBlock Text="SystemColors.InactiveBorderBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.InactiveCaptionBrushKey}}" >
<TextBlock Text="SystemColors.InactiveCaptionBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.InactiveCaptionTextBrushKey}}" >
<TextBlock Text="SystemColors.InactiveCaptionTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.InfoBrushKey}}" >
<TextBlock Text="SystemColors.InfoBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.InfoTextBrushKey}}" >
<TextBlock Text="SystemColors.InfoTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" >
<TextBlock Text="SystemColors.MenuBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}" >
<TextBlock Text="SystemColors.MenuBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}" >
<TextBlock Text="SystemColors.MenuHighlightBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" >
<TextBlock Text="SystemColors.MenuTextBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" >
<TextBlock Text="SystemColors.ScrollBarBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" >
<TextBlock Text="SystemColors.WindowBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" >
<TextBlock Text="SystemColors.WindowFrameBrushKey" />
</Border>
<Border Background="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" >
<TextBlock Text="SystemColors.WindowTextBrushKey" />
</Border>
</StackPanel>
</StackPanel>
</ScrollViewer>
感谢您的帮助
修改 这是发布的C#解决方案的VB.NET版本。 您可以绑定到WindowColorBrush属性以设置WPF样式中控件的背景颜色等内容:
Public Class WindowsBackgroundRetriever
Implements System.ComponentModel.INotifyPropertyChanged
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure DWMCOLORIZATIONPARAMS
Public ColorizationColor As UInt32
Public ColorizationAfterglow As UInt32
Public ColorizationColorBalance As UInt32
Public ColorizationAfterglowBalance As UInt32
Public ColorizationBlurBalance As UInt32
Public ColorizationGlassReflectionIntensity As UInt32
Public ColorizationOpaqueBlend As UInt32
End Structure
Private cParams As New DWMCOLORIZATIONPARAMS
<Runtime.InteropServices.DllImport("dwmapi.dll", EntryPoint:="#127")> _
Private Shared Sub DwmGetColorizationParameters(ByRef dp As DWMCOLORIZATIONPARAMS)
End Sub
Private _windowColor As Color
Public Property WindowColor As Color
Get
Return _windowColor
End Get
Set(ByVal value As Color)
_windowColor = value
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("WindowColor"))
End Set
End Property
Private _windowColorBrush As SolidColorBrush
Public Property WindowColorBrush As SolidColorBrush
Get
If _windowColorBrush Is Nothing Then
_windowColorBrush = New SolidColorBrush(WindowColor)
End If
Return _windowColorBrush
End Get
Set(ByVal value As SolidColorBrush)
_windowColorBrush = value
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("WindowColorBrush"))
End Set
End Property
Public Sub New()
DwmGetColorizationParameters(cParams)
Dim r = BitConverter.GetBytes(cParams.ColorizationColor >> 16)
Dim g = BitConverter.GetBytes(cParams.ColorizationColor >> 8)
Dim b = BitConverter.GetBytes(cParams.ColorizationColor)
WindowColor = Color.FromArgb(BitConverter.GetBytes(255)(0), r(0), g(0), b(0))
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
答案 0 :(得分:4)
您是否希望在Windows Vista和7中获取Aero玻璃色调?您可以使用this question中讨论的未记录的P / Invoke以及它链接到的其他一些页面来访问它,最重要的是this article。
具体来说,您需要使用以下Windows功能:
// In a static NativeMethods class
[DllImport("dwmapi.dll", EntryPoint = "#127")]
static extern void DwmGetColorizationParameters(ref DWMCOLORIZATIONPARAMS dp);
DWMCOLORIZATIONPARAMS
定义如下:
[StructLayout(LayoutKind.Sequential)]
public struct DWMCOLORIZATIONPARAMS
{
public UInt32 ColorizationColor;
public UInt32 ColorizationAfterglow;
public UInt32 ColorizationColorBalance;
public UInt32 ColorizationAfterglowBalance;
public UInt32 ColorizationBlurBalance;
public UInt32 ColorizationGlassReflectionIntensity;
public UInt32 ColorizationOpaqueBlend;
}
虽然这个结构看起来很多,但你只对它的ColorizationColor
感兴趣。
以下是我将该值转换为System.Windows.Media.Color
以便在WPF / Silverlight中使用的方法(所需要的只是少数几位变换):
var cParams = new DWMCOLORIZATIONPARAMS();
NativeMethods.DwmGetColorizationParameters(ref cParams);
var windowColor = Color.FromArgb(
(byte) 255,
(byte)(cParams.ColorizationColor >> 16),
(byte)(cParams.ColorizationColor >> 8),
(byte) cParams.ColorizationColor
);
链接问题带来的唯一问题是,它没有文档,所以它可以随时改变或消失(我甚至不确定这是否适用于Windows 8的桌面界面,但我相信应该)。但是,这种技术对我有用,它目前也在其他WPF应用程序中使用,如MetroTwit,效果惊人。实际上,我宁愿使用它而不是钻进注册表。