Mercurial > silverbladetech
annotate Chronosv2/source/Extensions/Windows/UIElementExtensions.cs @ 19:09d18d6e5f40
airport work
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Thu, 15 Mar 2012 06:59:15 +0000 |
parents | 443821e55f06 |
children |
rev | line source |
---|---|
19 | 1 using System.Windows; |
10
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
2 using System.Windows.Media; |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
3 using System.Windows.Media.Imaging; |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
4 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
5 namespace Chronos.Extensions.Windows |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
6 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
7 /// <summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
8 /// Extension methods for the System.Windows.UIElement class |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
9 /// </summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
10 public static class UIElementExtensions |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
11 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
12 #region · Extensions · |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
13 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
14 /// <summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
15 /// Gets the parent of an <see cref="UIElement"/>. |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
16 /// </summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
17 /// <typeparam name="T"></typeparam> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
18 /// <param name="element">The element.</param> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
19 /// <returns></returns> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
20 public static T GetParent<T>(this UIElement element) where T : UIElement |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
21 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
22 // Walk the VisualTree to obtain the DesktopItem of the DragThumb |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
23 DependencyObject parent = VisualTreeHelper.GetParent(element); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
24 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
25 while (parent != null && !(parent is T)) |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
26 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
27 parent = VisualTreeHelper.GetParent(parent); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
28 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
29 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
30 return parent as T; |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
31 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
32 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
33 /// <summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
34 /// Renders the ui element into a bitmap frame. |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
35 /// </summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
36 /// <param name="element">The UI element.</param> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
37 /// <returns>The created bitmap frame</returns> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
38 public static BitmapSource RenderToBitmap(this UIElement element) |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
39 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
40 return element.RenderToBitmap(1); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
41 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
42 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
43 /// <summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
44 /// Renders the ui element into a bitmap frame using the specified scale. |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
45 /// </summary> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
46 /// <param name="element">The UI element.</param> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
47 /// <param name="scale">The scale (default: 1).</param> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
48 /// <returns>The created bitmap frame</returns> |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
49 public static BitmapSource RenderToBitmap(this UIElement element, double scale) |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
50 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
51 var renderWidth = (int)(element.RenderSize.Width * scale); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
52 var renderHeight = (int)(element.RenderSize.Height * scale); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
53 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
54 var renderTarget = new RenderTargetBitmap(renderWidth, renderHeight, 96, 96, PixelFormats.Pbgra32); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
55 var sourceBrush = new VisualBrush(element); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
56 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
57 var drawingVisual = new DrawingVisual(); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
58 var drawingContext = drawingVisual.RenderOpen(); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
59 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
60 using (drawingContext) |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
61 { |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
62 drawingContext.PushTransform(new ScaleTransform(scale, scale)); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
63 drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(element.RenderSize.Width, element.RenderSize.Height))); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
64 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
65 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
66 renderTarget.Render(drawingVisual); |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
67 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
68 return renderTarget; |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
69 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
70 |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
71 #endregion |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
72 } |
443821e55f06
Initial cleaned up add from Codeplex files
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
73 } |