|
1 | | -using System.Windows; |
| 1 | +using System; |
| 2 | +using System.Windows; |
2 | 3 | using System.Windows.Controls; |
3 | 4 | using System.Windows.Controls.Primitives; |
4 | 5 | using System.Windows.Documents; |
5 | 6 | using System.Windows.Input; |
6 | 7 | using System.Windows.Media; |
7 | 8 | using System.Windows.Shapes; |
8 | | -using WPFDevelopers.Core; |
9 | 9 |
|
10 | 10 | namespace WPFDevelopers.Controls |
11 | 11 | { |
12 | 12 | public class ScreenCutAdorner : Adorner |
13 | 13 | { |
14 | | - private const double THUMB_SIZE = 15; |
| 14 | + private const double THUMB_SIZE = 10; |
15 | 15 | private const double MINIMAL_SIZE = 20; |
16 | 16 | private const double LINE_Size = 6; |
17 | 17 | private readonly Thumb lc; |
@@ -139,78 +139,91 @@ private Thumb GetResizeThumb(Cursor cur, HorizontalAlignment hor, VerticalAlignm |
139 | 139 | } |
140 | 140 | }; |
141 | 141 | } |
142 | | - |
143 | | - |
144 | | - var maxWidth = double.IsNaN(canvas.Width) ? canvas.ActualWidth : canvas.Width; |
145 | | - var maxHeight = double.IsNaN(canvas.Height) ? canvas.ActualHeight : canvas.Height; |
| 142 | + |
146 | 143 | thumb.DragDelta += (s, e) => |
147 | 144 | { |
148 | 145 | var element = AdornedElement as FrameworkElement; |
149 | 146 | if (element == null) |
150 | 147 | return; |
151 | 148 | Resize(element); |
152 | 149 |
|
| 150 | + if (double.IsNaN(Canvas.GetLeft(element))) |
| 151 | + Canvas.SetLeft(element, 0); |
| 152 | + if (double.IsNaN(Canvas.GetTop(element))) |
| 153 | + Canvas.SetTop(element, 0); |
| 154 | + |
| 155 | + double left = Canvas.GetLeft(element); |
| 156 | + double top = Canvas.GetTop(element); |
| 157 | + |
| 158 | + double canvasWidth = canvas.ActualWidth; |
| 159 | + double canvasHeight = canvas.ActualHeight; |
| 160 | + |
153 | 161 | switch (thumb.VerticalAlignment) |
154 | 162 | { |
155 | | - case VerticalAlignment.Bottom: |
156 | | - if (element.Height + e.VerticalChange > MINIMAL_SIZE) |
| 163 | + case VerticalAlignment.Top: |
157 | 164 | { |
158 | | - var newHeight = element.Height + e.VerticalChange; |
159 | | - var top = Canvas.GetTop(element) + newHeight; |
160 | | - if (newHeight > 0 && top <= canvas.ActualHeight) |
161 | | - { |
162 | | - element.Height = newHeight; |
163 | | - if(_isRatioScale) |
164 | | - ScaleWidth(thumb, element, newHeight); |
165 | | - } |
| 165 | + double newTop = top + e.VerticalChange; |
| 166 | + double maxTop = top + element.Height - MINIMAL_SIZE; |
| 167 | + |
| 168 | + newTop = Math.Max(0, Math.Min(newTop, maxTop)); |
| 169 | + |
| 170 | + double newHeight = element.Height + (top - newTop); |
| 171 | + |
| 172 | + Canvas.SetTop(element, newTop); |
| 173 | + element.Height = newHeight; |
| 174 | + |
| 175 | + if (_isRatioScale) |
| 176 | + ScaleWidth(thumb, element, newHeight); |
166 | 177 | } |
167 | 178 | break; |
168 | 179 |
|
169 | | - case VerticalAlignment.Top: |
170 | | - if (element.Height - e.VerticalChange > MINIMAL_SIZE) |
| 180 | + case VerticalAlignment.Bottom: |
171 | 181 | { |
172 | | - var newHeight = element.Height - e.VerticalChange; |
173 | | - var top = Canvas.GetTop(element); |
174 | | - if (newHeight > 0 && top + e.VerticalChange >= 0) |
175 | | - { |
176 | | - element.Height = newHeight; |
177 | | - Canvas.SetTop(element, top + e.VerticalChange); |
178 | | - if (_isRatioScale) |
179 | | - ScaleWidth(thumb, element, newHeight); |
180 | | - } |
181 | | - } |
| 182 | + double maxHeight = canvasHeight - top; |
| 183 | + |
| 184 | + double newHeight = element.Height + e.VerticalChange; |
| 185 | + newHeight = Math.Max(MINIMAL_SIZE, newHeight); |
| 186 | + newHeight = Math.Min(newHeight, maxHeight); |
182 | 187 |
|
| 188 | + element.Height = newHeight; |
| 189 | + |
| 190 | + if (_isRatioScale) |
| 191 | + ScaleWidth(thumb, element, newHeight); |
| 192 | + } |
183 | 193 | break; |
184 | 194 | } |
185 | 195 |
|
186 | 196 | switch (thumb.HorizontalAlignment) |
187 | 197 | { |
188 | 198 | case HorizontalAlignment.Left: |
189 | | - if (element.Width - e.HorizontalChange > MINIMAL_SIZE) |
190 | 199 | { |
191 | | - var newWidth = element.Width - e.HorizontalChange; |
192 | | - var left = Canvas.GetLeft(element); |
193 | | - if (newWidth > 0 && left + e.HorizontalChange >= 0) |
194 | | - { |
195 | | - element.Width = newWidth; |
196 | | - Canvas.SetLeft(element, left + e.HorizontalChange); |
197 | | - if (_isRatioScale) |
198 | | - ScaleHeight(thumb, element, newWidth); |
199 | | - } |
200 | | - } |
| 200 | + double newLeft = left + e.HorizontalChange; |
| 201 | + double maxLeft = left + element.Width - MINIMAL_SIZE; |
| 202 | + |
| 203 | + newLeft = Math.Max(0, Math.Min(newLeft, maxLeft)); |
| 204 | + |
| 205 | + double newWidth = element.Width + (left - newLeft); |
201 | 206 |
|
| 207 | + Canvas.SetLeft(element, newLeft); |
| 208 | + element.Width = newWidth; |
| 209 | + |
| 210 | + if (_isRatioScale) |
| 211 | + ScaleHeight(thumb, element, newWidth); |
| 212 | + } |
202 | 213 | break; |
| 214 | + |
203 | 215 | case HorizontalAlignment.Right: |
204 | | - if (element.Width + e.HorizontalChange > MINIMAL_SIZE) |
205 | 216 | { |
206 | | - var newWidth = element.Width + e.HorizontalChange; |
207 | | - var left = Canvas.GetLeft(element) + newWidth; |
208 | | - if (newWidth > 0 && left <= canvas.ActualWidth) |
209 | | - { |
210 | | - element.Width = newWidth; |
211 | | - if (_isRatioScale) |
212 | | - ScaleHeight(thumb, element, newWidth); |
213 | | - } |
| 217 | + double maxWidth = canvasWidth - left; |
| 218 | + |
| 219 | + double newWidth = element.Width + e.HorizontalChange; |
| 220 | + newWidth = Math.Max(MINIMAL_SIZE, newWidth); |
| 221 | + newWidth = Math.Min(newWidth, maxWidth); |
| 222 | + |
| 223 | + element.Width = newWidth; |
| 224 | + |
| 225 | + if (_isRatioScale) |
| 226 | + ScaleHeight(thumb, element, newWidth); |
214 | 227 | } |
215 | 228 | break; |
216 | 229 | } |
@@ -246,12 +259,12 @@ void ScaleWidth(Thumb thumb, FrameworkElement element, double newHeight) |
246 | 259 | void ScaleHeight(Thumb thumb, FrameworkElement element, double newWidth) |
247 | 260 | { |
248 | 261 | if (_isRatioScale |
249 | | - && |
250 | | - thumb.VerticalAlignment != VerticalAlignment.Top |
251 | | - || |
252 | | - (thumb.HorizontalAlignment != HorizontalAlignment.Left |
253 | | - && |
254 | | - thumb.HorizontalAlignment != HorizontalAlignment.Right)) |
| 262 | + && |
| 263 | + thumb.VerticalAlignment != VerticalAlignment.Top |
| 264 | + || |
| 265 | + (thumb.HorizontalAlignment != HorizontalAlignment.Left |
| 266 | + && |
| 267 | + thumb.HorizontalAlignment != HorizontalAlignment.Right)) |
255 | 268 | { |
256 | 269 | if (!_scaleSize.IsEmpty |
257 | 270 | && |
|
0 commit comments