markus / KCOM / Extensions / VisualHelper.cs @ c4a8d205
이력 | 보기 | 이력해설 | 다운로드 (4.79 KB)
1 |
using MarkupToPDF.Common; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using System.Windows; |
8 |
using System.Windows.Controls; |
9 |
using System.Windows.Media; |
10 |
|
11 |
namespace KCOM |
12 |
{ |
13 |
public static class VisualHelper |
14 |
{ |
15 |
public static Rect Bounds(this UIElement element, Visual parent) |
16 |
{ |
17 |
return element.TransformToVisual(parent).TransformBounds(new Rect(element.RenderSize)); |
18 |
} |
19 |
|
20 |
public static Rect Rect(this UIElement element) |
21 |
{ |
22 |
return new Rect(element.RenderSize); |
23 |
} |
24 |
|
25 |
public static Direction GetPointDirection(Point point,double angle) |
26 |
{ |
27 |
Direction direction = Direction.None; |
28 |
|
29 |
double xValue = point.X; |
30 |
double yValue = point.Y; |
31 |
|
32 |
if(0 > xValue && Math.Abs(xValue) > Math.Abs(yValue)) |
33 |
{ |
34 |
direction = Direction.Left; |
35 |
} |
36 |
|
37 |
if (0 < xValue && Math.Abs(xValue) > Math.Abs(yValue)) |
38 |
{ |
39 |
direction = Direction.Right; |
40 |
} |
41 |
|
42 |
if (0 > yValue && Math.Abs(xValue) < Math.Abs(yValue)) |
43 |
{ |
44 |
direction = Direction.Up; |
45 |
} |
46 |
|
47 |
if (0 < yValue && Math.Abs(xValue) < Math.Abs(yValue)) |
48 |
{ |
49 |
direction = Direction.Down; |
50 |
} |
51 |
|
52 |
return direction; |
53 |
} |
54 |
|
55 |
public static MoveDirection Movement(this Rect Owner,Rect contentRect) |
56 |
{ |
57 |
MoveDirection result = new MoveDirection(); |
58 |
|
59 |
if(contentRect.Top > 0) |
60 |
{ |
61 |
result.Up = true; |
62 |
} |
63 |
|
64 |
if(contentRect.Left > 0) |
65 |
{ |
66 |
result.Left = true; |
67 |
} |
68 |
|
69 |
if(Owner.Width > contentRect.Right) |
70 |
{ |
71 |
result.Right = true; |
72 |
} |
73 |
|
74 |
if (Owner.Height > contentRect.Bottom) |
75 |
{ |
76 |
result.Down = true; |
77 |
} |
78 |
|
79 |
return result; |
80 |
} |
81 |
|
82 |
public static IEnumerable<DependencyObject> FindAllChildren(this DependencyObject dpo, Predicate<DependencyObject> predicate) |
83 |
{ |
84 |
//var results = new List<DependencyObject>(); |
85 |
//if (predicate == null) |
86 |
// yield return results; |
87 |
|
88 |
|
89 |
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpo); i++) |
90 |
{ |
91 |
var child = VisualTreeHelper.GetChild(dpo, i); |
92 |
if (predicate(child)) |
93 |
yield return child;// results.Add(child); |
94 |
|
95 |
var subChildren = child.FindAllChildren(predicate); |
96 |
foreach (var item in subChildren) |
97 |
{ |
98 |
yield return item; |
99 |
} |
100 |
|
101 |
//results.AddRange(subChildren); |
102 |
} |
103 |
} |
104 |
|
105 |
public static IEnumerable<CommentUserInfo> FindComment(this System.Windows.Controls.Canvas canvas, Rect searchRect) |
106 |
{ |
107 |
foreach (CommentUserInfo child in canvas.FindAllChildren(x=>x is CommentUserInfo)) |
108 |
{ |
109 |
if (child is CommentUserInfo comment) |
110 |
{ |
111 |
if (searchRect.IntersectsWith(child.ItemRect)) |
112 |
{ |
113 |
yield return child; |
114 |
} |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
public static CommentUserInfo FindDistanceComment(this Canvas canvas, Point point,double squareSize = 1) |
120 |
{ |
121 |
double minDistance = double.MaxValue; |
122 |
CommentUserInfo closestComment = null; |
123 |
|
124 |
Rect searchRect = new Rect( |
125 |
point.X - squareSize, |
126 |
point.Y - squareSize, |
127 |
squareSize * 2, |
128 |
squareSize * 2 |
129 |
); |
130 |
|
131 |
foreach (CommentUserInfo child in canvas.FindComment(searchRect)) |
132 |
{ |
133 |
if (child is CommentUserInfo comment) |
134 |
{ |
135 |
double left = comment.ItemRect.Left; |
136 |
double top = comment.ItemRect.Top; |
137 |
double centerX = left + comment.ItemRect.Width / 2; |
138 |
double centerY = top + comment.ItemRect.Height / 2; |
139 |
double distance = Math.Sqrt(Math.Pow(point.X - centerX, 2) + Math.Pow(point.Y - centerY, 2)); |
140 |
|
141 |
if (distance < minDistance) |
142 |
{ |
143 |
minDistance = distance; |
144 |
closestComment = comment; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
return closestComment; |
150 |
} |
151 |
} |
152 |
|
153 |
public class MoveDirection |
154 |
{ |
155 |
public bool Left { get; set; } |
156 |
|
157 |
public bool Right { get; set; } |
158 |
|
159 |
public bool Up { get; set; } |
160 |
|
161 |
public bool Down { get; set; } |
162 |
} |
163 |
|
164 |
public enum Direction |
165 |
{ |
166 |
None, |
167 |
Left, |
168 |
|
169 |
Right, |
170 |
|
171 |
Up, |
172 |
|
173 |
Down |
174 |
} |
175 |
} |