簡體   English   中英

檢索Windows“窗口顏色”

[英]Retrieving the Windows “Window Color”

我正在研究WPF應用程序,我正在使用Telerik對話框控件,它顯示為Window,但實際上並不是Window對象。

因為它不是一個實際的窗口(但它看起來像一個窗口),它的樣式與我的應用程序中使用的其余對話框不匹配。

因此,我為此對話框創建了一個模板,我正在為系統顏色分配系統顏色,以便對話框使用相同的顏色,並且具有與普通窗口相同的外觀和感覺。

出於某種原因,我找不到“窗口顏色”的系統顏色(不是窗口背景顏色,那個很容易找到......我說的是實際的窗口顏色......窗口的框架和標題)

我已經瀏覽了所有的System.Windows.SystemColors,但找不到定義“Window Color”的系統顏色......就像我說的,我不想引用“窗口的背景顏色”。

我想知道是否有人知道如何檢索這種顏色。

編輯:這是我在嘗試確定哪種系統顏色用於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樣式中控件的Background顏色等內容:

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

您是否希望在Windows Vista和7中獲得Aero玻璃色調? 您可以使用此問題中討論的未記錄的P / Invoke以及它鏈接到的其他一些頁面來訪問它,最重要的是本文

具體來說,您需要使用以下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 ,效果驚人。 實際上,我寧願使用它而不是鑽進注冊表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM