WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法

在WPF中,在Win32颜色互相转换时,需要用到以下方法

SolidColorBrush转COLORREF

复制代码
1         public int ToCOLORREF(SolidColorBrush solidColorBrush)
2         {
3             var color = solidColorBrush.Color;
4             return ((color.R | (color.G << 8)) | (color.B << 0x10));
5         }

COLORREF转SolidColorBrush

复制代码
        public SolidColorBrush FromCOLORREF(int COLORREF_Color)
        {
            return new SolidColorBrush(Color.FromRgb((byte)(COLORREF_Color & 0xff), (byte)((COLORREF_Color >> 8) & 0xff), (byte)((COLORREF_Color >> 0x10) & 0xff)));
        }

说明:

也可以使用System.Drawing.ColorTranslator.ToOle和System.Drawing.ColorTranslator.FromOle方法来进行转换,但是不推荐这种方法,因为返回类型是System.Drawing.Color,需要再次转换才能在WPF中使用。

附:从16进制字符串转SolidColorBrush的方法

复制代码
1         public SolidColorBrush FromString(string colorStr)
2         {
3             return new SolidColorBrush((Color)ColorConverter.ConvertFromString(colorStr));
4         }
相关推荐
没有bug.的程序员2 天前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
Macbethad2 天前
基于WPF的半导体设备配方管理程序技术方案
wpf
FuckPatience2 天前
WPF Geometry
wpf
武藤一雄3 天前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
MarkHD3 天前
车辆TBOX科普 第70次 AUTOSAR Adaptive、容器化与云原生的融合革命
云原生·wpf
极客智造3 天前
WPF Behavior 实战:自定义 InvokeCommandAction 实现事件与命令解耦
wpf
L、2183 天前
Flutter 与 OpenHarmony 深度集成:构建分布式多端协同应用
分布式·flutter·wpf
布伦鸽3 天前
C# WPF -MaterialDesignTheme 找不到资源“xxx“问题记录
开发语言·c#·wpf
小二·3 天前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis
helloworddm4 天前
UnregisterManyAsync
wpf