markus / KCOM / Extensions / VisualHelper.cs @ a1e2ba68
이력 | 보기 | 이력해설 | 다운로드 (3.04 KB)
1 | 8e5a4a6a | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Windows; |
||
7 | using System.Windows.Media; |
||
8 | |||
9 | namespace KCOM |
||
10 | { |
||
11 | public static class VisualHelper |
||
12 | { |
||
13 | public static Rect Bounds(this UIElement element, Visual parent) |
||
14 | { |
||
15 | return element.TransformToVisual(parent).TransformBounds(new Rect(element.RenderSize)); |
||
16 | } |
||
17 | |||
18 | public static Rect Rect(this UIElement element) |
||
19 | { |
||
20 | return new Rect(element.RenderSize); |
||
21 | } |
||
22 | |||
23 | 3dbace4e | taeseongkim | public static Direction GetPointDirection(Point point,double angle) |
24 | { |
||
25 | Direction direction = Direction.None; |
||
26 | |||
27 | double xValue = point.X; |
||
28 | double yValue = point.Y; |
||
29 | |||
30 | if(0 > xValue && Math.Abs(xValue) > Math.Abs(yValue)) |
||
31 | { |
||
32 | direction = Direction.Left; |
||
33 | } |
||
34 | |||
35 | if (0 < xValue && Math.Abs(xValue) > Math.Abs(yValue)) |
||
36 | { |
||
37 | direction = Direction.Right; |
||
38 | } |
||
39 | |||
40 | if (0 > yValue && Math.Abs(xValue) < Math.Abs(yValue)) |
||
41 | { |
||
42 | direction = Direction.Up; |
||
43 | } |
||
44 | |||
45 | if (0 < yValue && Math.Abs(xValue) < Math.Abs(yValue)) |
||
46 | { |
||
47 | direction = Direction.Down; |
||
48 | } |
||
49 | |||
50 | return direction; |
||
51 | } |
||
52 | |||
53 | 8e5a4a6a | taeseongkim | public static MoveDirection Movement(this Rect Owner,Rect contentRect) |
54 | { |
||
55 | MoveDirection result = new MoveDirection(); |
||
56 | |||
57 | if(contentRect.Top > 0) |
||
58 | { |
||
59 | result.Up = true; |
||
60 | } |
||
61 | |||
62 | if(contentRect.Left > 0) |
||
63 | { |
||
64 | result.Left = true; |
||
65 | } |
||
66 | |||
67 | if(Owner.Width > contentRect.Right) |
||
68 | { |
||
69 | result.Right = true; |
||
70 | } |
||
71 | |||
72 | if (Owner.Height > contentRect.Bottom) |
||
73 | { |
||
74 | result.Down = true; |
||
75 | } |
||
76 | |||
77 | return result; |
||
78 | } |
||
79 | b74a9c91 | taeseongkim | |
80 | public static IEnumerable<DependencyObject> FindAllChildren(this DependencyObject dpo, Predicate<DependencyObject> predicate) |
||
81 | { |
||
82 | //var results = new List<DependencyObject>(); |
||
83 | //if (predicate == null) |
||
84 | // yield return results; |
||
85 | |||
86 | |||
87 | for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpo); i++) |
||
88 | { |
||
89 | var child = VisualTreeHelper.GetChild(dpo, i); |
||
90 | if (predicate(child)) |
||
91 | yield return child;// results.Add(child); |
||
92 | |||
93 | var subChildren = child.FindAllChildren(predicate); |
||
94 | foreach (var item in subChildren) |
||
95 | { |
||
96 | yield return item; |
||
97 | } |
||
98 | |||
99 | //results.AddRange(subChildren); |
||
100 | } |
||
101 | } |
||
102 | 8e5a4a6a | taeseongkim | } |
103 | |||
104 | public class MoveDirection |
||
105 | { |
||
106 | public bool Left { get; set; } |
||
107 | |||
108 | public bool Right { get; set; } |
||
109 | |||
110 | public bool Up { get; set; } |
||
111 | |||
112 | public bool Down { get; set; } |
||
113 | } |
||
114 | 3dbace4e | taeseongkim | |
115 | public enum Direction |
||
116 | { |
||
117 | None, |
||
118 | Left, |
||
119 | |||
120 | Right, |
||
121 | |||
122 | Up, |
||
123 | |||
124 | Down |
||
125 | } |
||
126 | 8e5a4a6a | taeseongkim | } |