While putting together what hopefully will be a future post, I came across a situation where I needed to know what the red, green, and blue values were for various system colors. On a Windows Mobile device, those values are stored in the Registry. In fact, when a user changes the Today theme via the Today Settings applet, behind the scenes that specific registry value is being changed.
All the information in the following paragraph comes from this entry on MSDN.
System Colors are stored in the SysColor registry value in the HKEY_LOCAL_MACHINE\System\GWE key. GWE is the Graphics, Windows, and Events subsystem. There are 29 different colors which are stored in a blob in the SysColor key. These values control items such as caption background and text colors, button colors, and window and menu colors.
Below is a C# class which allows easy retrieval and access to those System Colors. Part 2 will extend the class to save new values to the System Colors.
These are images of a simple application used to retrieve the system colors and set the foreground color of list view items to the corresponding colors.
Image 1 - Before Retrieving System Colors
Image 2 - After Retrieving System Colors
Image 3 - After Changing to Guava Bubbles Today Theme and Retrieving System Colors
#region Using Directives
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Security;
using System.Text;
using Microsoft.Win32;
#endregion
namespace SystemColors
{
public static class SystemColors
{
#region MSDN Information
// Taken from "Customizing System Colors" (http://msdn2.microsoft.com/en-us/library/ms906614.aspx)
//0 COLOR_SCROLLBAR Color of the gray area of a scroll bar.
//1 COLOR_BACKGROUND Background color of the desktop window.
//2 COLOR_ACTIVECAPTION Color of the title bar of an active window.
//3 COLOR_INACTIVECAPTION Color of the title bar of an inactive window.
//4 COLOR_MENU Background color of a menu.
//5 COLOR_WINDOW Background color of a window.
//6 COLOR_WINDOWFRAME Color of a window frame.
//7 COLOR_MENUTEXT Color of the text in a menu.
//8 COLOR_WINDOWTEXT Color of the text in a window.
//9 COLOR_CAPTIONTEXT Color of the text in a title bar and of the size box and scroll bar arrow box.
//10 COLOR_ACTIVEBORDER Color of the border of an active window.
//11 COLOR_INACTIVEBORDER Color of the border of an inactive window.
//12 COLOR_APPWORKSPACE Background color of multiple document interface (MDI) applications.
//13 COLOR_HIGHLIGHT Color of an item selected in a control.
//14 COLOR_HIGHLIGHTTEXT Color of the text of an item selected in a control.
//15 COLOR_BTNFACE Color of the face of a button.
//16 COLOR_BTNSHADOW Shadow color of buttons for edges that face away from the light source.
//17 COLOR_GRAYTEXT Color of shaded text. This color is set to 0 if the current display driver does not support a solid gray color.
//18 COLOR_BTNTEXT Color of the text for push buttons.
//19 COLOR_INACTIVECAPTIONTEXT Color of the text in the title bar of an inactive window.
//20 COLOR_BTNHIGHLIGHT Highlight color of buttons for edges that face the light source.
//21 COLOR_3DDKSHADOW Color of the dark shadow for three-dimensional display elements.
//22 COLOR_3DLIGHT Highlight color of three-dimensional display elements for edges that face the light source.
//23 COLOR_INFOTEXT Color of the text for ToolTip controls.
//24 COLOR_INFOBK Background color for ToolTip controls.
//25 COLOR_STATIC Background color for static controls and dialog boxes. Supported in Windows CE 2.0 and later.
//26 COLOR_STATICTEXT Color of the text for static controls. Supported in Windows CE 2.0 and later.
//27 COLOR_GRADIENTACTIVECAPTION Color of the title bar of an active window that is filled with a color gradient.
//28 COLOR_GRADIENTINACTIVECAPTION Color of the title bar of an inactive window that is filled with a color gradient.
#endregion
#region Constants
private const string SYSTEM_SUBKEY = "SYSTEM";
private const string GWE_SUBKEY = "GWE";
private const string SYSTEM_COLORS_KEY = SYSTEM_SUBKEY + SLASH + GWE_SUBKEY;
private const string SLASH = @"\";
private const string SYSTEM_COLORS_VALUE = "SysColor";
private const int SYSTEM_COLORS_BLOB_SIZE = 116; // 29 (# of values) * 4 (size of DWORD)
private const int SCROLLBAR_OFFSET = 0; // 0 (position) * 4 (size of DWORD)
private const int BACKGROUND_OFFSET = 4; // 1 (position) * 4 (size of DWORD)
private const int ACTIVECAPTION_OFFSET = 8; // 2 (position) * 4 (size of DWORD)
private const int INACTIVECAPTION_OFFSET = 12; // 3 (position) * 4 (size of DWORD)
private const int MENU_OFFSET = 16; // 4 (position) * 4 (size of DWORD)
private const int WINDOW_OFFSET = 20; // 5 (position) * 4 (size of DWORD)
private const int WINDOWFRAME_OFFSET = 24; // 6 (position) * 4 (size of DWORD)
private const int MENUTEXT_OFFSET = 28; // 7 (position) * 4 (size of DWORD)
private const int WINDOWTEXT_OFFSET = 32; // 8 (position) * 4 (size of DWORD)
private const int CAPTIONTEXT_OFFSET = 36; // 9 (position) * 4 (size of DWORD)
private const int ACTIVEBORDER_OFFSET = 40; // 10 (position) * 4 (size of DWORD)
private const int INACTIVEBORDER_OFFSET = 44; // 11 (position) * 4 (size of DWORD)
private const int APPWORKSPACE_OFFSET = 48; // 12 (position) * 4 (size of DWORD)
private const int HIGHLIGHT_OFFSET = 52; // 13 (position) * 4 (size of DWORD)
private const int HIGHLIGHTTEXT_OFFSET = 56; // 14 (position) * 4 (size of DWORD)
private const int BTNFACE_OFFSET = 60; // 15 (position) * 4 (size of DWORD)
private const int BTNSHADOW_OFFSET = 64; // 16 (position) * 4 (size of DWORD)
private const int GRAYTEXT_OFFSET = 68; // 17 (position) * 4 (size of DWORD)
private const int BTNTEXT_OFFSET = 72; // 18 (position) * 4 (size of DWORD)
private const int INACTIVECAPTIONTEXT_OFFSET = 76; // 19 (position) * 4 (size of DWORD)
private const int BTNHIGHLIGHT_OFFSET = 80; // 20 (position) * 4 (size of DWORD)
private const int THREEDDKSHADOW_OFFSET = 84; // 21 (position) * 4 (size of DWORD)
private const int THREEDLIGHT_OFFSET = 88; // 22 (position) * 4 (size of DWORD)
private const int INFOTEXT_OFFSET = 92; // 23 (position) * 4 (size of DWORD)
private const int INFOBK_OFFSET = 96; // 24 (position) * 4 (size of DWORD)
private const int STATIC_OFFSET = 100; // 25 (position) * 4 (size of DWORD)
private const int STATICTEXT_OFFSET = 104; // 26 (position) * 4 (size of DWORD)
private const int GRADIENTACTIVECAPTION_OFFSET = 108; // 27 (position) * 4 (size of DWORD)
private const int GRADIENTINACTIVECAPTION_OFFSET = 112; // 28 (position) * 4 (size of DWORD)
#endregion
#region Static Fields
private static Color m_scrollbar;
private static Color m_background;
private static Color m_activeCaption;
private static Color m_inactiveCaption;
private static Color m_menu;
private static Color m_window;
private static Color m_windowFrame;
private static Color m_menuText;
private static Color m_windowText;
private static Color m_captionText;
private static Color m_activeBorder;
private static Color m_inactiveBorder;
private static Color m_appWorkspace;
private static Color m_highlight;
private static Color m_highlightText;
private static Color m_buttonFace;
private static Color m_buttonShadow;
private static Color m_grayText;
private static Color m_buttonText;
private static Color m_inactiveCaptionText;
private static Color m_buttonHighlight;
private static Color m_threeDimensionalDarkShadow;
private static Color m_threeDimensionalLight;
private static Color m_infoText;
private static Color m_infoBackground;
private static Color m_static;
private static Color m_staticText;
private static Color m_gradientActiveCaption;
private static Color m_gradientInactiveCaption;
#endregion
#region Private Static Methods
private static Color ConvertToSystemColor(byte[] values, int offset)
{
if (values.Length >= (offset + 4))
{
int r = (int)values[offset];
int g = (int)values[offset + 1];
int b = (int)values[offset + 2];
return Color.FromArgb(r, g, b);
}
else
{
throw new SystemColorsException("Incorrect System Color Values Received");
}
}
private static void ParseSystemColors(byte[] systemColors)
{
// Make sure size is proper
if (systemColors.Length == SYSTEM_COLORS_BLOB_SIZE)
{
m_scrollbar = ConvertToSystemColor(systemColors, SCROLLBAR_OFFSET);
m_background = ConvertToSystemColor(systemColors, BACKGROUND_OFFSET);
m_activeCaption = ConvertToSystemColor(systemColors, ACTIVECAPTION_OFFSET);
m_inactiveCaption = ConvertToSystemColor(systemColors, INACTIVECAPTION_OFFSET);
m_menu = ConvertToSystemColor(systemColors, MENU_OFFSET);
m_window = ConvertToSystemColor(systemColors, WINDOW_OFFSET);
m_windowFrame = ConvertToSystemColor(systemColors, WINDOWFRAME_OFFSET);
m_menuText = ConvertToSystemColor(systemColors, MENUTEXT_OFFSET);
m_windowText = ConvertToSystemColor(systemColors, WINDOWTEXT_OFFSET);
m_captionText = ConvertToSystemColor(systemColors, CAPTIONTEXT_OFFSET);
m_activeBorder = ConvertToSystemColor(systemColors, ACTIVEBORDER_OFFSET);
m_inactiveBorder = ConvertToSystemColor(systemColors, INACTIVEBORDER_OFFSET);
m_appWorkspace = ConvertToSystemColor(systemColors, APPWORKSPACE_OFFSET);
m_highlight = ConvertToSystemColor(systemColors, HIGHLIGHT_OFFSET);
m_highlightText = ConvertToSystemColor(systemColors, HIGHLIGHTTEXT_OFFSET);
m_buttonFace = ConvertToSystemColor(systemColors, BTNFACE_OFFSET);
m_buttonShadow = ConvertToSystemColor(systemColors, BTNSHADOW_OFFSET);
m_grayText = ConvertToSystemColor(systemColors, GRAYTEXT_OFFSET);
m_buttonText = ConvertToSystemColor(systemColors, BTNTEXT_OFFSET);
m_inactiveCaptionText = ConvertToSystemColor(systemColors, INACTIVECAPTIONTEXT_OFFSET);
m_buttonHighlight = ConvertToSystemColor(systemColors, BTNHIGHLIGHT_OFFSET);
m_threeDimensionalDarkShadow = ConvertToSystemColor(systemColors, THREEDDKSHADOW_OFFSET);
m_threeDimensionalLight = ConvertToSystemColor(systemColors, THREEDLIGHT_OFFSET);
m_infoText = ConvertToSystemColor(systemColors, INFOTEXT_OFFSET);
m_infoBackground = ConvertToSystemColor(systemColors, INFOBK_OFFSET);
m_static = ConvertToSystemColor(systemColors, STATIC_OFFSET);
m_staticText = ConvertToSystemColor(systemColors, STATICTEXT_OFFSET);
m_gradientActiveCaption = ConvertToSystemColor(systemColors, GRADIENTACTIVECAPTION_OFFSET);
m_gradientInactiveCaption = ConvertToSystemColor(systemColors, GRADIENTINACTIVECAPTION_OFFSET);
}
}
private static void LoadSystemColors(bool throwExceptions)
{
RegistryKey key = null;
try
{
key = Registry.LocalMachine.OpenSubKey(SYSTEM_COLORS_KEY);
byte[] value = (byte[])key.GetValue(SYSTEM_COLORS_VALUE);
ParseSystemColors(value);
}
catch (UnauthorizedAccessException uae)
{
if (throwExceptions)
{
throw new SystemColorsException(uae.Message, uae);
}
}
catch (IOException ioe)
{
if (throwExceptions)
{
throw new SystemColorsException(ioe.Message, ioe);
}
}
catch (SecurityException se)
{
if (throwExceptions)
{
throw new SystemColorsException(se.Message, se);
}
}
catch (ArgumentNullException ane)
{
if (throwExceptions)
{
throw new SystemColorsException(ane.Message, ane);
}
}
catch (ArgumentException ae)
{
if (throwExceptions)
{
throw new SystemColorsException(ae.Message, ae);
}
}
catch (ObjectDisposedException ode)
{
if (throwExceptions)
{
throw new SystemColorsException(ode.Message, ode);
}
}
catch (SystemColorsException sce)
{
if (throwExceptions)
{
throw sce;
}
}
finally
{
if (key != null)
{
try
{
key.Close();
key = null;
}
catch (Exception ex)
{
if (throwExceptions)
{
throw new SystemColorsException(ex.Message, ex);
}
}
}
}
}
#endregion
#region Public Static Methods
public static void LoadSystemColors()
{
LoadSystemColors(true);
}
public static void LoadSystemColorsSilently()
{
LoadSystemColors(false);
}
public static void SaveSystemColors()
{
}
#endregion
#region Static Properties
///
/// Color of the gray area of a scroll bar.
///
public static Color Scrollbar
{
get { return m_scrollbar; }
set { m_scrollbar = value; }
}
///
/// Background color of the desktop window.
///
public static Color Background
{
get { return m_background; }
set { m_background = value; }
}
///
/// Color of the title bar of an active window.
///
public static Color ActiveCaption
{
get { return m_activeCaption; }
set { m_activeCaption = value; }
}
///
/// Color of the title bar of an inactive window.
///
public static Color InactiveCaption
{
get { return m_inactiveCaption; }
set { m_inactiveCaption = value; }
}
///
/// Background color of a menu.
///
public static Color Menu
{
get { return m_menu; }
set { m_menu = value; }
}
///
/// Background color of a window.
///
public static Color Window
{
get { return m_window; }
set { m_window = value; }
}
///
/// Color of a window frame.
///
public static Color WindowFrame
{
get { return m_windowFrame; }
set { m_windowFrame = value; }
}
///
/// Color of the text in a menu.
///
public static Color MenuText
{
get { return m_menuText; }
set { m_menuText = value; }
}
///
/// Color of the text in a window.
///
public static Color WindowText
{
get { return m_windowText; }
set { m_windowText = value; }
}
///
/// Color of the text in a title bar and of the size box and scroll bar arrow box.
///
public static Color CaptionText
{
get { return m_captionText; }
set { m_captionText = value; }
}
///
/// Color of the border of an active window.
///
public static Color ActiveBorder
{
get { return m_activeBorder; }
set { m_activeBorder = value; }
}
///
/// Color of the border of an inactive window.
///
public static Color InactiveBorder
{
get { return m_inactiveBorder; }
set { m_inactiveBorder = value; }
}
///
/// Background color of multiple document interface (MDI) applications.
///
public static Color AppWorkspace
{
get { return m_appWorkspace; }
set { m_appWorkspace = value; }
}
///
/// Color of an item selected in a control.
///
public static Color Highlight
{
get { return m_highlight; }
set { m_highlight = value; }
}
///
/// Color of the text of an item selected in a control.
///
public static Color HighlightText
{
get { return m_highlightText; }
set { m_highlightText = value; }
}
///
/// Color of the face of a button.
///
public static Color ButtonFace
{
get { return m_buttonFace; }
set { m_buttonFace = value; }
}
///
/// Shadow color of buttons for edges that face away from the light source.
///
public static Color ButtonShadow
{
get { return m_buttonShadow; }
set { m_buttonShadow = value; }
}
///
/// Color of shaded text. This color is set to 0 if the current display driver does not support a solid gray color.
///
public static Color GrayText
{
get { return m_grayText; }
set { m_grayText = value; }
}
///
/// Color of the text for push buttons.
///
public static Color ButtonText
{
get { return m_buttonText; }
set { m_buttonText = value; }
}
///
/// Color of the text in the title bar of an inactive window.
///
public static Color InactiveCaptionText
{
get { return m_inactiveCaptionText; }
set { m_inactiveCaptionText = value; }
}
///
/// Highlight color of buttons for edges that face the light source.
///
public static Color ButtonHighlight
{
get { return m_buttonHighlight; }
set { m_buttonHighlight = value; }
}
///
/// Color of the dark shadow for three-dimensional display elements.
///
public static Color ThreeDimensionalDarkShadow
{
get { return m_threeDimensionalDarkShadow; }
set { m_threeDimensionalDarkShadow = value; }
}
///
/// Highlight color of three-dimensional display elements for edges that face the light source.
///
public static Color ThreeDimensionalLight
{
get { return m_threeDimensionalLight; }
set { m_threeDimensionalLight = value; }
}
///
/// Color of the text for ToolTip controls.
///
public static Color InfoText
{
get { return m_infoText; }
set { m_infoText = value; }
}
///
/// Background color for ToolTip controls.
///
public static Color InfoBackground
{
get { return m_infoBackground; }
set { m_infoBackground = value; }
}
///
/// Background color for static controls and dialog boxes. Supported in Windows CE 2.0 and later.
///
public static Color Static
{
get { return m_static; }
set { m_static = value; }
}
///
/// Color of the text for static controls. Supported in Windows CE 2.0 and later.
///
public static Color StaticText
{
get { return m_staticText; }
set { m_staticText = value; }
}
///
/// Color of the title bar of an active window that is filled with a color gradient.
///
public static Color GradientActiveCaption
{
get { return m_gradientActiveCaption; }
set { m_gradientActiveCaption = value; }
}
///
/// Color of the title bar of an inactive window that is filled with a color gradient.
///
public static Color GradientInactiveCaption
{
get { return m_gradientInactiveCaption; }
set { m_gradientInactiveCaption = value; }
}
#endregion
}
}
1 comment:
Post a Comment