2 // VisualStyleRenderer.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Novell, Inc.
26 // Jonathan Pobst (monkey@jpobst.com)
31 namespace System.Windows.Forms.VisualStyles
33 public sealed class VisualStyleRenderer
35 private string class_name;
39 private int last_hresult = 0;
40 private ThemeHandleManager theme_handle_manager = new ThemeHandleManager ();
42 #region Public Constructors
43 public VisualStyleRenderer (string className, int part, int state)
45 theme_handle_manager.VisualStyleRenderer = this;
46 this.SetParameters (className, part, state);
49 public VisualStyleRenderer (VisualStyleElement element)
50 : this (element.ClassName, element.Part, element.State) {
54 #region Public Properties
55 public String Class { get { return this.class_name; } }
56 public IntPtr Handle { get { return this.theme; } }
57 public int LastHResult { get { return this.last_hresult; } }
58 public int Part { get { return this.part; } }
59 public int State { get { return this.state; } }
61 public static bool IsSupported {
63 if (!VisualStyleInformation.IsEnabledByUser)
66 if (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled ||
67 Application.VisualStyleState == VisualStyleState.ClientAreaEnabled)
75 #region Public Static Methods
76 public static bool IsElementDefined (VisualStyleElement element)
79 throw new InvalidOperationException ("Visual Styles are not enabled.");
81 if (IsElementKnownToBeSupported (element.ClassName, element.Part, element.State))
84 IntPtr theme = VisualStyles.UxThemeOpenThemeData (IntPtr.Zero, element.ClassName);
85 if (theme == IntPtr.Zero)
87 bool retval = VisualStyles.UxThemeIsThemePartDefined (theme, element.Part);
88 VisualStyles.UxThemeCloseThemeData (theme);
94 #region Public Instance Methods
95 public void DrawBackground (IDeviceContext dc, Rectangle bounds)
98 throw new ArgumentNullException ("dc");
100 last_hresult = VisualStyles.UxThemeDrawThemeBackground (theme, dc, this.part, this.state, bounds);
103 public void DrawBackground (IDeviceContext dc, Rectangle bounds, Rectangle clipRectangle)
106 throw new ArgumentNullException ("dc");
108 last_hresult = VisualStyles.UxThemeDrawThemeBackground (theme, dc, this.part, this.state, bounds, clipRectangle);
111 public Rectangle DrawEdge (IDeviceContext dc, Rectangle bounds, Edges edges, EdgeStyle style, EdgeEffects effects)
114 throw new ArgumentNullException ("dc");
117 last_hresult = VisualStyles.UxThemeDrawThemeEdge (theme, dc, this.part, this.state, bounds, edges, style, effects, out result);
121 public void DrawImage (Graphics g, Rectangle bounds, ImageList imageList, int imageIndex)
124 throw new ArgumentNullException ("g");
125 if (imageIndex < 0 || imageIndex > imageList.Images.Count - 1)
126 throw new ArgumentOutOfRangeException ("imageIndex");
127 if (imageList.Images[imageIndex] == null)
128 throw new ArgumentNullException ("imageIndex");
130 g.DrawImage (imageList.Images[imageIndex], bounds);
133 public void DrawImage (Graphics g, Rectangle bounds, Image image)
136 throw new ArgumentNullException ("g");
138 throw new ArgumentNullException ("image");
140 g.DrawImage (image, bounds);
143 public void DrawParentBackground (IDeviceContext dc, Rectangle bounds, Control childControl)
146 throw new ArgumentNullException ("dc");
148 last_hresult = VisualStyles.UxThemeDrawThemeParentBackground (dc, bounds, childControl);
151 public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw, bool drawDisabled, TextFormatFlags flags)
154 throw new ArgumentNullException ("dc");
156 last_hresult = VisualStyles.UxThemeDrawThemeText (theme, dc, this.part, this.state, textToDraw, flags, bounds);
159 public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw, bool drawDisabled)
161 this.DrawText (dc, bounds, textToDraw, drawDisabled, TextFormatFlags.Default);
164 public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw)
166 this.DrawText (dc, bounds, textToDraw, false, TextFormatFlags.Default);
169 public Rectangle GetBackgroundContentRectangle (IDeviceContext dc, Rectangle bounds)
172 throw new ArgumentNullException ("dc");
175 last_hresult = VisualStyles.UxThemeGetThemeBackgroundContentRect (theme, dc, this.part, this.state, bounds, out result);
179 public Rectangle GetBackgroundExtent (IDeviceContext dc, Rectangle contentBounds)
182 throw new ArgumentNullException ("dc");
185 last_hresult = VisualStyles.UxThemeGetThemeBackgroundExtent (theme, dc, this.part, this.state, contentBounds, out result);
189 [System.Security.SuppressUnmanagedCodeSecurity]
190 public Region GetBackgroundRegion (IDeviceContext dc, Rectangle bounds)
193 throw new ArgumentNullException ("dc");
196 last_hresult = VisualStyles.UxThemeGetThemeBackgroundRegion (theme, dc, this.part, this.state, bounds, out result);
200 public bool GetBoolean (BooleanProperty prop)
202 if (!Enum.IsDefined (typeof (BooleanProperty), prop))
203 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (BooleanProperty));
206 last_hresult = VisualStyles.UxThemeGetThemeBool (theme, this.part, this.state, prop, out result);
210 public Color GetColor (ColorProperty prop)
212 if (!Enum.IsDefined (typeof (ColorProperty), prop))
213 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (ColorProperty));
216 last_hresult = VisualStyles.UxThemeGetThemeColor (theme, this.part, this.state, prop, out result);
220 public int GetEnumValue (EnumProperty prop)
222 if (!Enum.IsDefined (typeof (EnumProperty), prop))
223 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (EnumProperty));
226 last_hresult = VisualStyles.UxThemeGetThemeEnumValue (theme, this.part, this.state, prop, out result);
230 public string GetFilename (FilenameProperty prop)
232 if (!Enum.IsDefined (typeof (FilenameProperty), prop))
233 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (FilenameProperty));
236 last_hresult = VisualStyles.UxThemeGetThemeFilename (theme, this.part, this.state, prop, out result);
241 [MonoTODO(@"I can't get MS's to return anything but null, so I can't really get this one right")]
242 public Font GetFont (IDeviceContext dc, FontProperty prop)
244 throw new NotImplementedException();
246 // throw new ArgumentNullException ("dc");
247 //if (!Enum.IsDefined (typeof (FontProperty), prop))
248 // throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (FontProperty));
250 //UXTheme.LOGFONT lf = new UXTheme.LOGFONT();
252 //UXTheme.GetThemeFont (theme, dc.GetHdc (), this.part, this.state, (int)prop, out lf);
253 //IntPtr fontPtr = UXTheme.CreateFontIndirect(lf);
256 //return Font.FromLogFont(lf);
260 public int GetInteger (IntegerProperty prop)
262 if (!Enum.IsDefined (typeof (IntegerProperty), prop))
263 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (IntegerProperty));
266 last_hresult = VisualStyles.UxThemeGetThemeInt (theme, this.part, this.state, prop, out result);
270 [MonoTODO(@"MS's causes a PInvokeStackUnbalance on me, so this is not verified against MS.")]
271 public Padding GetMargins (IDeviceContext dc, MarginProperty prop)
274 throw new ArgumentNullException ("dc");
275 if (!Enum.IsDefined (typeof (MarginProperty), prop))
276 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (MarginProperty));
279 last_hresult = VisualStyles.UxThemeGetThemeMargins (theme, dc, this.part, this.state, prop, out result);
283 public Size GetPartSize (IDeviceContext dc, Rectangle bounds, ThemeSizeType type)
286 throw new ArgumentNullException ("dc");
287 if (!Enum.IsDefined (typeof (ThemeSizeType), type))
288 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)type, typeof (ThemeSizeType));
291 last_hresult = VisualStyles.UxThemeGetThemePartSize (theme, dc, this.part, this.state, bounds, type, out result);
295 public Size GetPartSize (IDeviceContext dc, ThemeSizeType type)
298 throw new ArgumentNullException ("dc");
299 if (!Enum.IsDefined (typeof (ThemeSizeType), type))
300 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)type, typeof (ThemeSizeType));
303 last_hresult = VisualStyles.UxThemeGetThemePartSize (theme, dc, this.part, this.state, type, out result);
307 public Point GetPoint (PointProperty prop)
309 if (!Enum.IsDefined (typeof (PointProperty), prop))
310 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (PointProperty));
313 last_hresult = VisualStyles.UxThemeGetThemePosition (theme, this.part, this.state, prop, out result);
317 [MonoTODO(@"Can't find any values that return anything on MS to test against")]
318 public string GetString (StringProperty prop)
320 if (!Enum.IsDefined (typeof (StringProperty), prop))
321 throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (StringProperty));
324 last_hresult = VisualStyles.UxThemeGetThemeString (theme, this.part, this.state, prop, out result);
328 public Rectangle GetTextExtent (IDeviceContext dc, Rectangle bounds, string textToDraw, TextFormatFlags flags)
331 throw new ArgumentNullException ("dc");
334 last_hresult = VisualStyles.UxThemeGetThemeTextExtent (theme, dc, this.part, this.state, textToDraw, flags, bounds, out result);
338 public Rectangle GetTextExtent (IDeviceContext dc, string textToDraw, TextFormatFlags flags)
341 throw new ArgumentNullException ("dc");
344 last_hresult = VisualStyles.UxThemeGetThemeTextExtent (theme, dc, this.part, this.state, textToDraw, flags, out result);
348 public TextMetrics GetTextMetrics (IDeviceContext dc)
351 throw new ArgumentNullException ("dc", "dc cannot be null.");
354 last_hresult = VisualStyles.UxThemeGetThemeTextMetrics (theme, dc, this.part, this.state, out result);
358 public HitTestCode HitTestBackground (IDeviceContext dc, Rectangle backgroundRectangle, IntPtr hRgn, Point pt, HitTestOptions options)
361 throw new ArgumentNullException ("dc");
364 last_hresult = VisualStyles.UxThemeHitTestThemeBackground(theme, dc, this.part, this.state, options, backgroundRectangle, hRgn, pt, out result);
368 public HitTestCode HitTestBackground (Graphics g, Rectangle backgroundRectangle, Region region, Point pt, HitTestOptions options)
371 throw new ArgumentNullException ("g");
373 IntPtr hRgn = region.GetHrgn(g);
375 return this.HitTestBackground(g, backgroundRectangle, hRgn, pt, options);
378 public HitTestCode HitTestBackground (IDeviceContext dc, Rectangle backgroundRectangle, Point pt, HitTestOptions options)
380 return this.HitTestBackground (dc, backgroundRectangle, IntPtr.Zero, pt, options);
383 public bool IsBackgroundPartiallyTransparent ()
385 return VisualStyles.UxThemeIsThemeBackgroundPartiallyTransparent (theme, this.part, this.state);
388 public void SetParameters (string className, int part, int state)
390 if (theme != IntPtr.Zero)
391 last_hresult = VisualStyles.UxThemeCloseThemeData (theme);
394 throw new InvalidOperationException ("Visual Styles are not enabled.");
396 this.class_name = className;
399 theme = VisualStyles.UxThemeOpenThemeData (IntPtr.Zero, this.class_name);
401 if (IsElementKnownToBeSupported (className, part, state))
403 if (theme == IntPtr.Zero || !VisualStyles.UxThemeIsThemePartDefined (theme, this.part))
404 throw new ArgumentException ("This element is not supported by the current visual style.");
407 public void SetParameters (VisualStyleElement element)
409 this.SetParameters (element.ClassName, element.Part, element.State);
413 #region Private Properties
414 internal static IVisualStyles VisualStyles {
415 get { return VisualStylesEngine.Instance; }
419 #region Private Instance Methods
420 internal void DrawBackgroundExcludingArea (IDeviceContext dc, Rectangle bounds, Rectangle excludedArea)
422 VisualStyles.VisualStyleRendererDrawBackgroundExcludingArea (theme, dc, part, state, bounds, excludedArea);
426 #region Private Static Methods
427 private static bool IsElementKnownToBeSupported (string className, int part, int state)
429 return className == "STATUS" && part == 0 && state == 0;
433 #region Private Classes
434 private class ThemeHandleManager
436 public VisualStyleRenderer VisualStyleRenderer;
437 ~ThemeHandleManager ()
439 if (VisualStyleRenderer.theme == IntPtr.Zero)
441 VisualStyles.UxThemeCloseThemeData (VisualStyleRenderer.theme);