-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizingAdorner.cs
258 lines (207 loc) · 11.4 KB
/
ResizingAdorner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace SkinText {
public class ResizingAdorner : Adorner {
// Resizing adorner uses Thumbs for visual elements.
// The Thumbs have built-in mouse input handling.
private Thumb topLeft, topRight, bottomLeft, bottomRight;
private readonly
// To store and manage the adorner's visual children.
VisualCollection visualChildren;
// Initialize the ResizingAdorner.
public ResizingAdorner(UIElement adornedElement)
: base(adornedElement) {
visualChildren = new VisualCollection(this);
// Call a helper method to initialize the Thumbs
// with a customized cursors.
BuildAdornerCorner(ref topLeft, Cursors.SizeNWSE);
BuildAdornerCorner(ref topRight, Cursors.SizeNESW);
BuildAdornerCorner(ref bottomLeft, Cursors.SizeNESW);
BuildAdornerCorner(ref bottomRight, Cursors.SizeNWSE);
// Add handlers for resizing.
bottomLeft.DragDelta += HandleBottomLeft;
bottomRight.DragDelta += HandleBottomRight;
topLeft.DragDelta += HandleTopLeft;
topRight.DragDelta += HandleTopRight;
}
// Handler for resizing from the bottom-right.
private void HandleBottomRight(object sender, DragDeltaEventArgs args) {
FrameworkElement adornedElement = this.AdornedElement as FrameworkElement;
Thumb hitThumb = sender as Thumb;
if (adornedElement == null || hitThumb == null) {
return;
}
FrameworkElement parentElement = adornedElement.Parent as FrameworkElement;
// Ensure that the Width and Height are properly initialized after the resize.
EnforceSize(adornedElement);
// Change the size by the amount the user drags the mouse, as long as it's larger
// than the width or height of an adorner, respectively.
double newWidth = Math.Max(adornedElement.Width + args.HorizontalChange, hitThumb.DesiredSize.Width);
if (newWidth > ((Canvas)adornedElement.Parent).ActualWidth - Canvas.GetLeft(adornedElement))
{
newWidth = ((Canvas)adornedElement.Parent).ActualWidth - Canvas.GetLeft(adornedElement);
}
adornedElement.Width = newWidth;
double newHeight = Math.Max(args.VerticalChange + adornedElement.Height, hitThumb.DesiredSize.Height);
if (newHeight > ((Canvas)adornedElement.Parent).ActualHeight - Canvas.GetTop(adornedElement))
{
newHeight = ((Canvas)adornedElement.Parent).ActualHeight - Canvas.GetTop(adornedElement);
}
adornedElement.Height = newHeight;
}
// Handler for resizing from the top-right.
private void HandleTopRight(object sender, DragDeltaEventArgs args) {
FrameworkElement adornedElement = this.AdornedElement as FrameworkElement;
Thumb hitThumb = sender as Thumb;
if (adornedElement == null || hitThumb == null) {
return;
}
FrameworkElement parentElement = adornedElement.Parent as FrameworkElement;
// Ensure that the Width and Height are properly initialized after the resize.
EnforceSize(adornedElement);
// Change the size by the amount the user drags the mouse, as long as it's larger
// than the width or height of an adorner, respectively.
double newWidth = Math.Max(adornedElement.Width + args.HorizontalChange, hitThumb.DesiredSize.Width);
if (newWidth > ((Canvas)adornedElement.Parent).ActualWidth - Canvas.GetLeft(adornedElement))
{
newWidth = ((Canvas)adornedElement.Parent).ActualWidth - Canvas.GetLeft(adornedElement);
}
adornedElement.Width = newWidth;
//adornedElement.Height = Math.Max(adornedElement.Height - args.VerticalChange, hitThumb.DesiredSize.Height);
double height_old = adornedElement.Height;
double height_new = Math.Max(adornedElement.Height - args.VerticalChange, hitThumb.DesiredSize.Height);
double top_old = Canvas.GetTop(adornedElement);
double newtop = top_old - (height_new - height_old);
if (newtop < 0)
{
newtop = 0;
height_new = height_old;
}
adornedElement.Height = height_new;
Canvas.SetTop(adornedElement, newtop);
}
// Handler for resizing from the top-left.
private void HandleTopLeft(object sender, DragDeltaEventArgs args) {
FrameworkElement adornedElement = AdornedElement as FrameworkElement;
Thumb hitThumb = sender as Thumb;
if (adornedElement == null || hitThumb == null) {
return;
}
// Ensure that the Width and Height are properly initialized after the resize.
EnforceSize(adornedElement);
// Change the size by the amount the user drags the mouse, as long as it's larger
// than the width or height of an adorner, respectively.
//adornedElement.Width = Math.Max(adornedElement.Width - args.HorizontalChange, hitThumb.DesiredSize.Width);
//adornedElement.Height = Math.Max(adornedElement.Height - args.VerticalChange, hitThumb.DesiredSize.Height);
double width_old = adornedElement.Width;
double width_new = Math.Max(adornedElement.Width - args.HorizontalChange, hitThumb.DesiredSize.Width);
double left_old = Canvas.GetLeft(adornedElement);
double newleft = left_old - (width_new - width_old);
if (newleft < 0)
{
newleft = 0;
width_new = width_old;
}
adornedElement.Width = width_new;
Canvas.SetLeft(adornedElement, newleft);
double height_old = adornedElement.Height;
double height_new = Math.Max(adornedElement.Height - args.VerticalChange, hitThumb.DesiredSize.Height);
double top_old = Canvas.GetTop(adornedElement);
double newtop = top_old - (height_new - height_old);
if (newtop < 0)
{
newtop = 0;
height_new = height_old;
}
adornedElement.Height = height_new;
Canvas.SetTop(adornedElement, newtop);
}
// Handler for resizing from the bottom-left.
private void HandleBottomLeft(object sender, DragDeltaEventArgs args) {
FrameworkElement adornedElement = AdornedElement as FrameworkElement;
Thumb hitThumb = sender as Thumb;
if (adornedElement == null || hitThumb == null) {
return;
}
// Ensure that the Width and Height are properly initialized after the resize.
EnforceSize(adornedElement);
// Change the size by the amount the user drags the mouse, as long as it's larger
// than the width or height of an adorner, respectively.
//adornedElement.Width = Math.Max(adornedElement.Width - args.HorizontalChange, hitThumb.DesiredSize.Width);
double newHeight = Math.Max(args.VerticalChange + adornedElement.Height, hitThumb.DesiredSize.Height);
if (newHeight> ((Canvas)adornedElement.Parent).ActualHeight - Canvas.GetTop(adornedElement))
{
newHeight = ((Canvas)adornedElement.Parent).ActualHeight - Canvas.GetTop(adornedElement);
}
adornedElement.Height = newHeight;
double width_old = adornedElement.Width;
double width_new = Math.Max(adornedElement.Width - args.HorizontalChange, hitThumb.DesiredSize.Width);
double left_old = Canvas.GetLeft(adornedElement);
double newleft = left_old - (width_new - width_old);
if (newleft<0)
{
newleft = 0;
width_new = width_old;
}
adornedElement.Width = width_new;
Canvas.SetLeft(adornedElement, newleft);
}
// Arrange the Adorners.
protected override Size ArrangeOverride(Size finalSize) {
// desiredWidth and desiredHeight are the width and height of the element that's being adorned.
// These will be used to place the ResizingAdorner at the corners of the adorned element.
double desiredWidth = AdornedElement.DesiredSize.Width;
double desiredHeight = AdornedElement.DesiredSize.Height;
// adornerWidth & adornerHeight are used for placement as well.
double adornerWidth = this.DesiredSize.Width;
double adornerHeight = this.DesiredSize.Height;
topLeft.Arrange(new Rect(-adornerWidth / 2, -adornerHeight / 2, adornerWidth, adornerHeight));
topRight.Arrange(new Rect(desiredWidth - adornerWidth / 2, -adornerHeight / 2, adornerWidth, adornerHeight));
bottomLeft.Arrange(new Rect(-adornerWidth / 2, desiredHeight - adornerHeight / 2, adornerWidth, adornerHeight));
bottomRight.Arrange(new Rect(desiredWidth - adornerWidth / 2, desiredHeight - adornerHeight / 2, adornerWidth, adornerHeight));
// Return the final size.
return finalSize;
}
// Helper method to instantiate the corner Thumbs, set the Cursor property,
// set some appearance properties, and add the elements to the visual tree.
private void BuildAdornerCorner(ref Thumb cornerThumb, Cursor customizedCursor) {
if (cornerThumb != null) {
return;
}
cornerThumb = new Thumb {
// Set some arbitrary visual characteristics.
Cursor = customizedCursor,
Opacity = 0.40,
Background = new SolidColorBrush(Colors.MediumBlue),
Height = 10,
Width = 10
};
visualChildren.Add(cornerThumb);
}
// This method ensures that the Widths and Heights are initialized. Sizing to content produces
// Width and Height values of Double.NaN. Because this Adorner explicitly resizes, the Width and Height
// need to be set first. It also sets the maximum size of the adorned element.
private void EnforceSize(FrameworkElement adornedElement) {
if (adornedElement.Width.Equals(double.NaN)) {
adornedElement.Width = adornedElement.DesiredSize.Width;
}
if (adornedElement.Height.Equals(double.NaN)) {
adornedElement.Height = adornedElement.DesiredSize.Height;
}
FrameworkElement parent = adornedElement.Parent as FrameworkElement;
if (parent != null) {
adornedElement.MaxHeight = parent.ActualHeight;
adornedElement.MaxWidth = parent.ActualWidth;
}
}
// Override the VisualChildrenCount and GetVisualChild properties to interface with
// the adorner's visual collection.
protected override int VisualChildrenCount => visualChildren.Count;
protected override Visual GetVisualChild(int index) => visualChildren[index];
}
}