|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Controls.Primitives; |
| 5 | +using System.Windows.Input; |
| 6 | +using System.Windows.Media; |
| 7 | +using System.Windows.Media.Animation; |
| 8 | +using System.Windows.Media.Imaging; |
| 9 | +using System.Windows.Shapes; |
| 10 | + |
| 11 | +namespace WPFDevelopers.Controls |
| 12 | +{ |
| 13 | + [TemplatePart(Name = UniformGridTemplateName, Type = typeof(UniformGrid))] |
| 14 | + public class CropControl: Control |
| 15 | + { |
| 16 | + private const string UniformGridTemplateName = "PART_UniformGrid"; |
| 17 | + |
| 18 | + private UniformGrid _uniformGrid; |
| 19 | + |
| 20 | + public ImageSource ImageSource |
| 21 | + { |
| 22 | + get { return (ImageSource)GetValue(ImageSourceProperty); } |
| 23 | + set { SetValue(ImageSourceProperty, value); } |
| 24 | + } |
| 25 | + |
| 26 | + public static readonly DependencyProperty ImageSourceProperty = |
| 27 | + DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(CropControl), new PropertyMetadata(null)); |
| 28 | + |
| 29 | + public int RowColumn |
| 30 | + { |
| 31 | + get { return (int)GetValue(RowColumnProperty); } |
| 32 | + set { SetValue(RowColumnProperty, value); } |
| 33 | + } |
| 34 | + |
| 35 | + public static readonly DependencyProperty RowColumnProperty = |
| 36 | + DependencyProperty.Register("RowColumn", typeof(int), typeof(CropControl), new PropertyMetadata(3)); |
| 37 | + |
| 38 | + static CropControl() |
| 39 | + { |
| 40 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(CropControl), new FrameworkPropertyMetadata(typeof(CropControl))); |
| 41 | + } |
| 42 | + public override void OnApplyTemplate() |
| 43 | + { |
| 44 | + base.OnApplyTemplate(); |
| 45 | + _uniformGrid = GetTemplateChild(UniformGridTemplateName) as UniformGrid; |
| 46 | + if (ImageSource == null || _uniformGrid == null) return; |
| 47 | + |
| 48 | + BitmapSource imgSource = (BitmapSource)ImageSource; |
| 49 | + int w = 0, h = 0; |
| 50 | + if (!imgSource.PixelWidth.Equals(0) |
| 51 | + && |
| 52 | + !imgSource.PixelHeight.Equals(0)) |
| 53 | + { |
| 54 | + w = imgSource.PixelWidth / RowColumn; |
| 55 | + h = (int)imgSource.PixelHeight / RowColumn; |
| 56 | + _uniformGrid.Width = imgSource.PixelWidth; |
| 57 | + _uniformGrid.Height = imgSource.PixelHeight; |
| 58 | + } |
| 59 | + for (int i = 0; i < RowColumn; i++) |
| 60 | + { |
| 61 | + for (int j = 0; j < RowColumn; j++) |
| 62 | + { |
| 63 | + var rect = new Rectangle |
| 64 | + { |
| 65 | + Fill = new ImageBrush { ImageSource = new CroppedBitmap(imgSource, new Int32Rect(j * w, i * h, w, h)) }, |
| 66 | + StrokeThickness = .5, |
| 67 | + Stroke = Brushes.White, |
| 68 | + Cursor = Cursors.Hand |
| 69 | + }; |
| 70 | + rect.RenderTransformOrigin = new Point(.5, .5); |
| 71 | + rect.RenderTransform = new ScaleTransform(); |
| 72 | + rect.MouseMove += (sender, ex) => |
| 73 | + { |
| 74 | + var rect1 = sender as Rectangle; |
| 75 | + Panel.SetZIndex(rect1, 1); |
| 76 | + var doubleAnimation = new DoubleAnimation |
| 77 | + { |
| 78 | + To = 2, |
| 79 | + Duration = TimeSpan.FromMilliseconds(100), |
| 80 | + }; |
| 81 | + var scaleTransform = rect1.RenderTransform as ScaleTransform; |
| 82 | + scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, doubleAnimation); |
| 83 | + scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, doubleAnimation); |
| 84 | + }; |
| 85 | + rect.MouseLeave += (sender, ex) => |
| 86 | + { |
| 87 | + var rect1 = sender as Rectangle; |
| 88 | + Panel.SetZIndex(rect1, 0); |
| 89 | + var scaleTransform = rect1.RenderTransform as ScaleTransform; |
| 90 | + var doubleAnimation = new DoubleAnimation |
| 91 | + { |
| 92 | + To = 1, |
| 93 | + Duration = TimeSpan.FromMilliseconds(100) |
| 94 | + }; |
| 95 | + scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, doubleAnimation); |
| 96 | + scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, doubleAnimation); |
| 97 | + }; |
| 98 | + _uniformGrid.Children.Add(rect); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments