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         }
相关推荐
码界奇点9 小时前
基于eBPF技术的高性能网络防火墙系统设计与实现
开发语言·网络·毕业设计·php·wpf·go语言·源代码管理
cjp56013 小时前
022.WPF 封装TextBox控件限制只输入数字自定义属性
wpf
cjp56013 小时前
021.WPF 以MVVM模式控制combox控件显示/隐藏
wpf
小北方城市网1 天前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf
流水线上的指令侠1 天前
补充说明——针对《C#:从 0 到 1 创建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目》
功能测试·ui·c#·自动化·wpf
流水线上的指令侠1 天前
C# 实战:从 0 到 1 搭建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目
功能测试·ui·c#·自动化·wpf·visual studio
贾修行1 天前
.NET 全栈开发学习路线:从入门到分布式
c#·.net·wpf·asp.net core·web api·winforms·services
晓13132 天前
第四章:Redis实战应用及常见问题(下篇)
java·数据库·缓存·wpf
掘根2 天前
【jsonRpc项目】客户端的Requestor模块,RpcCaller模块
wpf
FuckPatience3 天前
WPF ListBoxItem绑定自己在ListBox中的顺序
wpf