diff --git a/src/Common/src/Interop/Interop.COLORREF.cs b/src/Common/src/Interop/Interop.COLORREF.cs new file mode 100644 index 00000000000..cfc0fa0384b --- /dev/null +++ b/src/Common/src/Interop/Interop.COLORREF.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Drawing; + +internal partial class Interop +{ + /// + /// Helpers for color conversion. + /// + internal static class COLORREF + { + public static int RgbToCOLORREF(int rgbValue) + { + // Clear the A value, swap R & B values. + int bValue = (rgbValue & 0xFF) << 16; + + rgbValue &= 0xFFFF00; + rgbValue |= (rgbValue >> 16) & 0xFF; + rgbValue &= 0x00FFFF; + rgbValue |= bValue; + return rgbValue; + } + + public static Color COLORREFToColor(int colorref) + { + int r = colorref & 0xFF; + int g = (colorref >> 8) & 0xFF; + int b = (colorref >> 16) & 0xFF; + return Color.FromArgb(r, g, b); + } + + public static int ColorToCOLORREF(Color color) + => color.R | (color.G << 8) | (color.B << 16); + } +} diff --git a/src/Common/src/SafeNativeMethods.cs b/src/Common/src/SafeNativeMethods.cs index 26d7e33999a..6d622d93973 100644 --- a/src/Common/src/SafeNativeMethods.cs +++ b/src/Common/src/SafeNativeMethods.cs @@ -214,32 +214,6 @@ public static class VisualStyleSystemProperty [DllImport(ExternDll.Uxtheme, CharSet = CharSet.Auto)] public extern static int SetWindowTheme(IntPtr hWnd, string subAppName, string subIdList); - - // Color conversion - public static int RGBToCOLORREF(int rgbValue) - { - // clear the A value, swap R & B values - int bValue = (rgbValue & 0xFF) << 16; - - rgbValue &= 0xFFFF00; - rgbValue |= ((rgbValue >> 16) & 0xFF); - rgbValue &= 0x00FFFF; - rgbValue |= bValue; - return rgbValue; - } - - public static Color ColorFromCOLORREF(int colorref) - { - int r = colorref & 0xFF; - int g = (colorref >> 8) & 0xFF; - int b = (colorref >> 16) & 0xFF; - return Color.FromArgb(r, g, b); - } - - public static int ColorToCOLORREF(Color color) - { - return (int)color.R | ((int)color.G << 8) | ((int)color.B << 16); - } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ListViewInsertionMark.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ListViewInsertionMark.cs index 9835ace1dcd..e1114a69448 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ListViewInsertionMark.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ListViewInsertionMark.cs @@ -75,7 +75,7 @@ public Color Color { if (color.IsEmpty) { - color = SafeNativeMethods.ColorFromCOLORREF((int)listView.SendMessage((int)LVM.GETINSERTMARKCOLOR, 0, 0)); + color = COLORREF.COLORREFToColor((int)listView.SendMessage((int)LVM.GETINSERTMARKCOLOR, 0, 0)); } return color; } @@ -86,7 +86,7 @@ public Color Color color = value; if (listView.IsHandleCreated) { - listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, SafeNativeMethods.ColorToCOLORREF(color)); + listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, COLORREF.ColorToCOLORREF(color)); } } } @@ -139,7 +139,7 @@ internal void UpdateListView() if (!color.IsEmpty) { - listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, SafeNativeMethods.ColorToCOLORREF(color)); + listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, COLORREF.ColorToCOLORREF(color)); } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridEntry.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridEntry.cs index 303ef93a93f..905e10adcba 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridEntry.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridEntry.cs @@ -2485,8 +2485,8 @@ public virtual void PaintValue(object val, Graphics g, Rectangle rect, Rectangle try { - oldTextColor = Gdi32.SetTextColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(textColor.ToArgb())); - oldBkColor = Gdi32.SetBkColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(bkColor.ToArgb())); + oldTextColor = Gdi32.SetTextColor(new HandleRef(g, hdc), COLORREF.RgbToCOLORREF(textColor.ToArgb())); + oldBkColor = Gdi32.SetBkColor(new HandleRef(g, hdc), COLORREF.RgbToCOLORREF(bkColor.ToArgb())); hfont = Gdi32.SelectObject(hdc, hfont); User32.DT format = User32.DT.EDITCONTROL | User32.DT.EXPANDTABS | User32.DT.NOCLIP | User32.DT.SINGLELINE | User32.DT.NOPREFIX; if (gridHost.DrawValuesRightToLeft)