Winform程序颜色随系统颜色变化

来源:互联网 发布:初级会计职称软件 编辑:程序博客网 时间:2024/04/24 15:53

Winform程序颜色随系统颜色变化

参考【Windows 10 应用开发】跟随系统主题颜色

在复制下面类的时候要先添加几个引用
这里写图片描述

以下是这些引用的位置:

C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.WindowsRuntime\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.WindowsRuntime.dll
C:\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.Foundation.UniversalApiContract\5.0.0.0\Windows.Foundation.UniversalApiContract.winmd
C:\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract\1.0.0.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract.winmd

具体怎么添加引用就自行百度了.
以下是我写的类的代码,你也可以直接自己调用系统类来获得颜色,只需要转换一下两个不同的Color

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using Windows.UI.ViewManagement;namespace Harvest.View{    public class UwpColor    {        public UwpColor()        {            GetSystemColor();        }        /// <summary>        /// 强调色        /// </summary>        public Color Accent;        /// <summary>        /// 深的强调色        /// </summary>        public Color AccentDark1;        /// <summary>        /// 较深的强调色        /// </summary>        public Color AccentDark2;        /// <summary>        /// 最深的强调色        /// </summary>        public Color AccentDark3;        /// <summary>        /// 浅的强调色        /// </summary>        public Color AccentLight1;        /// <summary>        /// 较浅的强调色        /// </summary>        public Color AccentLight2;        /// <summary>        /// 最浅的强调色        /// </summary>        public Color AccentLight3;        /// <summary>        /// 背景色        /// </summary>        public Color Background;        /// <summary>        /// 前景色        /// </summary>        public Color Foreground;        /// <summary>        /// 获取系统当前颜色        /// </summary>        public void GetSystemColor()        {            #region 获取系统颜色            UISettings uISettings = new UISettings();            Windows.UI.Color temp;            temp = uISettings.GetColorValue(UIColorType.Accent);            Accent = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentDark1);            AccentDark1 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentDark2);            AccentDark2 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentDark3);            AccentDark3 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentLight1);            AccentLight1 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentLight2);            AccentLight2 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.AccentLight3);            AccentLight3 = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.Background);            Background = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            temp = uISettings.GetColorValue(UIColorType.Foreground);            Foreground = Color.FromArgb(temp.A, temp.R, temp.G, temp.B);            #endregion        }    }}

以下是这个类的使用方法:

            UwpColor uwpColor = new UwpColor();            uwpColor.GetSystemColor();            label1.BackColor = uwpColor.AccentDark3;