VisualStyleInformation.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // VisualStyleInformation.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Novell, Inc.
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. using System.Drawing;
  29. #if !NET_2_0
  30. using System.Runtime.InteropServices;
  31. #endif
  32. namespace System.Windows.Forms.VisualStyles
  33. {
  34. #if NET_2_0
  35. public static
  36. #endif
  37. class VisualStyleInformation
  38. {
  39. #if !NET_2_0
  40. static readonly Color system_colors_button_highlight;
  41. // These are taken from System.Drawing.
  42. internal enum GetSysColorIndex {
  43. COLOR_BTNHIGHLIGHT = 20,
  44. }
  45. // Windows values are in BGR format and without alpha
  46. // so we force it to opaque (or everything will be transparent) and reverse B and R
  47. static uint GetSysColor (GetSysColorIndex index)
  48. {
  49. uint bgr = Win32GetSysColor (index);
  50. return 0xFF000000 | (bgr & 0xFF) << 16 | (bgr & 0xFF00) | (bgr >> 16);
  51. }
  52. [DllImport ("user32.dll", EntryPoint = "GetSysColor", CallingConvention = CallingConvention.StdCall)]
  53. static extern uint Win32GetSysColor (GetSysColorIndex index);
  54. static VisualStyleInformation ()
  55. {
  56. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  57. system_colors_button_highlight = Color.FromArgb ((int)GetSysColor (GetSysColorIndex.COLOR_BTNHIGHLIGHT));
  58. else
  59. system_colors_button_highlight = Color.FromArgb (0xFF, 0xFF, 0xFF, 0xFF);
  60. }
  61. #endif
  62. #region Public Static Properties
  63. public static string Author {
  64. get {
  65. if (!VisualStyleRenderer.IsSupported)
  66. return string.Empty;
  67. return GetData ("AUTHOR");
  68. }
  69. }
  70. public static string ColorScheme {
  71. get {
  72. if (!VisualStyleRenderer.IsSupported)
  73. return string.Empty;
  74. Text.StringBuilder ThemeName = new Text.StringBuilder (260);
  75. Text.StringBuilder ColorName = new Text.StringBuilder (260);
  76. Text.StringBuilder SizeName = new Text.StringBuilder (260);
  77. UXTheme.GetCurrentThemeName (ThemeName, ThemeName.Capacity, ColorName, ColorName.Capacity, SizeName, SizeName.Capacity);
  78. return ColorName.ToString ();
  79. }
  80. }
  81. public static string Company {
  82. get {
  83. if (!VisualStyleRenderer.IsSupported)
  84. return string.Empty;
  85. return GetData ("COMPANY");
  86. }
  87. }
  88. [MonoTODO(@"Cannot get this to return the same as MS's...")]
  89. public static Color ControlHighlightHot {
  90. get {
  91. if (!VisualStyleRenderer.IsSupported)
  92. return
  93. #if NET_2_0
  94. SystemColors.ButtonHighlight
  95. #else
  96. system_colors_button_highlight
  97. #endif
  98. ;
  99. IntPtr theme = UXTheme.OpenThemeData (IntPtr.Zero, "BUTTON");
  100. uint retval = UXTheme.GetThemeSysColor (theme, 1621);
  101. UXTheme.CloseThemeData (theme);
  102. return System.Drawing.Color.FromArgb ((int)(0x000000FFU & retval),
  103. (int)(0x0000FF00U & retval) >> 8, (int)(0x00FF0000U & retval) >> 16);
  104. }
  105. }
  106. public static string Copyright {
  107. get {
  108. if (!VisualStyleRenderer.IsSupported)
  109. return string.Empty;
  110. return GetData ("COPYRIGHT");
  111. }
  112. }
  113. public static string Description {
  114. get {
  115. if (!VisualStyleRenderer.IsSupported)
  116. return string.Empty;
  117. return GetData ("DESCRIPTION");
  118. }
  119. }
  120. public static string DisplayName {
  121. get {
  122. if (!VisualStyleRenderer.IsSupported)
  123. return string.Empty;
  124. return GetData ("DISPLAYNAME");
  125. }
  126. }
  127. public static bool IsEnabledByUser {
  128. get {
  129. if (!VisualStyleInformation.IsSupportedByOS)
  130. return false;
  131. return (UXTheme.IsAppThemed () && UXTheme.IsThemeActive ());
  132. }
  133. }
  134. public static bool IsSupportedByOS {
  135. get {
  136. // Supported OS's should be NT based and at least XP (XP, 2003, Vista)
  137. if ((Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version >= new Version (5, 1)))
  138. return true;
  139. return false;
  140. }
  141. }
  142. public static int MinimumColorDepth {
  143. get {
  144. if (!VisualStyleRenderer.IsSupported)
  145. return 0;
  146. IntPtr theme = UXTheme.OpenThemeData (IntPtr.Zero, "BUTTON");
  147. int retval;
  148. UXTheme.GetThemeSysInt (theme, 1301, out retval);
  149. UXTheme.CloseThemeData (theme);
  150. return retval;
  151. }
  152. }
  153. public static string Size {
  154. get {
  155. if (!VisualStyleRenderer.IsSupported)
  156. return string.Empty;
  157. Text.StringBuilder ThemeName = new Text.StringBuilder (260);
  158. Text.StringBuilder ColorName = new Text.StringBuilder (260);
  159. Text.StringBuilder SizeName = new Text.StringBuilder (260);
  160. UXTheme.GetCurrentThemeName (ThemeName, ThemeName.Capacity, ColorName, ColorName.Capacity, SizeName, SizeName.Capacity);
  161. return SizeName.ToString ();
  162. }
  163. }
  164. public static bool SupportsFlatMenus {
  165. get {
  166. if (!VisualStyleRenderer.IsSupported)
  167. return false;
  168. IntPtr theme = UXTheme.OpenThemeData (IntPtr.Zero, "BUTTON");
  169. bool retval;
  170. retval = UXTheme.GetThemeSysBool (theme, 1001) == 0 ? false : true;
  171. UXTheme.CloseThemeData (theme);
  172. return retval;
  173. }
  174. }
  175. [MonoTODO(@"Cannot get this to return the same as MS's...")]
  176. public static Color TextControlBorder {
  177. get {
  178. if (!VisualStyleRenderer.IsSupported)
  179. return SystemColors.ControlDarkDark;
  180. IntPtr theme = UXTheme.OpenThemeData (IntPtr.Zero, "EDIT");
  181. uint retval = UXTheme.GetThemeSysColor (theme, 1611);
  182. UXTheme.CloseThemeData (theme);
  183. return System.Drawing.Color.FromArgb ((int)(0x000000FFU & retval),
  184. (int)(0x0000FF00U & retval) >> 8, (int)(0x00FF0000U & retval) >> 16);
  185. }
  186. }
  187. public static string Url {
  188. get {
  189. if (!VisualStyleRenderer.IsSupported)
  190. return string.Empty;
  191. return GetData ("URL");
  192. }
  193. }
  194. public static string Version {
  195. get {
  196. if (!VisualStyleRenderer.IsSupported)
  197. return string.Empty;
  198. return GetData ("VERSION");
  199. }
  200. }
  201. #endregion
  202. #region Internal Helper Methods
  203. internal static string GetData (string propertyName)
  204. {
  205. Text.StringBuilder ThemeName = new Text.StringBuilder (260);
  206. Text.StringBuilder ColorName = new Text.StringBuilder (260);
  207. Text.StringBuilder SizeName = new Text.StringBuilder (260);
  208. UXTheme.GetCurrentThemeName (ThemeName, ThemeName.Capacity, ColorName, ColorName.Capacity, SizeName, SizeName.Capacity);
  209. Text.StringBuilder PropertyValue = new Text.StringBuilder (260);
  210. UXTheme.GetThemeDocumentationProperty (ThemeName.ToString(), propertyName, PropertyValue, PropertyValue.Capacity);
  211. return PropertyValue.ToString ();
  212. }
  213. #endregion
  214. }
  215. }