码迷,mamicode.com
首页 > Windows程序 > 详细

WPF 捕获系统主题改变

时间:2020-06-15 14:05:01      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:previous   desktop   update   sof   for   call   manager   doc   pre   

Language Version: C# 8.0

WPF 捕获系统主题改变

通过System.Windows.Interop

using System.Windows.Interop;

// this is Window 可以写在构造方法里
    this.SourceInitialized += Theme.ThemeManager.Window_SourceInitialized;

// see "https://stackoverflow.com/questions/13660976/get-the-active-color-of-windows-8-automatic-color-theme"
void Window_SourceInitialized(object sender, EventArgs e)
{
    IntPtr hwnd;
    if ((hwnd = new WindowInteropHelper(sender as Window).Handle) == IntPtr.Zero)
        throw new InvalidOperationException("Could not get window handle.");

    HwndSource.FromHwnd(hwnd).AddHook(WndProc);
}

const int WM_DWMCOLORIZATIONCOLORCHANGED = 0x320;
// see "https://stackoverflow.com/questions/13660976/get-the-active-color-of-windows-8-automatic-color-theme"
IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_DWMCOLORIZATIONCOLORCHANGED:
            /* 
             * Update gradient brushes with new color information from
             * NativeMethods.DwmGetColorizationParams() or the registry.
             */

            return IntPtr.Zero;
        default:
            return IntPtr.Zero;
    }
}

这个也可以捕获系统颜色主题改变 也是WM_DWMCOLORIZATIONCOLORCHANGED

WPF 捕获系统主题改变

标签:previous   desktop   update   sof   for   call   manager   doc   pre   

原文地址:https://www.cnblogs.com/frg2089/p/13130178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!