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         }
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘8 小时前
开发WPF项目时遇到的问题总结
wpf
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars1 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest1 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0072 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业5 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103586 天前
WPF之绑定!
c#·wpf
DataIntel6 天前
wpf问题记录
wpf
蓝点lilac7 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像