//按下鼠标 private void UserControl_MouseDown(object sender, MouseButtonEventArgs e) { this.MouseLocation = MouseLocationEnum.None; if (e.OriginalSource.GetType() == typeof(Rectangle)) { Rectangle Act = e.OriginalSource as Rectangle; switch (Act.Name) { case "R_Left": MouseLocation = MouseLocationEnum.Left; break; case "R_LeftUp": MouseLocation = MouseLocationEnum.LeftUp; break; case "R_Up": MouseLocation = MouseLocationEnum.Up; break; case "R_RightUp": MouseLocation = MouseLocationEnum.RightUp; break; case "R_Right": MouseLocation = MouseLocationEnum.Right; break; case "R_RightDown": MouseLocation = MouseLocationEnum.RightDown; break; case "R_Down": MouseLocation = MouseLocationEnum.Down; break; case "R_LeftDown": MouseLocation = MouseLocationEnum.LeftDown; break; default: MouseLocation = MouseLocationEnum.None; break; } this.Action = MouseActionEx.Drag; } else { this.MouseDownPoint = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法 this.MouseDownLocate = this.ImageArea.TransformToAncestor((UIElement)this.MainGrid).Transform(new Point(0, 0)); if ((this.MouseDownLocate.X < this.MouseDownPoint.X && this.MouseDownPoint.X < this.MouseDownLocate.X + this.ImageArea.ActualWidth) && (this.MouseDownLocate.Y < this.MouseDownPoint.Y && this.MouseDownPoint.Y < this.MouseDownLocate.Y + this.ImageArea.ActualHeight) ) { this.Action = MouseActionEx.DragMove; } } }
//鼠标离开 private void UserControl_MouseLeave(object sender, MouseEventArgs e) { this.Action = MouseActionEx.None; }
//弹起鼠标 private void UserControl_MouseUp(object sender, MouseButtonEventArgs e) { this.Action = MouseActionEx.None; this.Cursor = Cursors.Arrow; }
//移动鼠标 private void UserControl_MouseMove(object sender, MouseEventArgs e) { //鼠标相对空间区域位置 Point MousePoint = e.GetPosition((IInputElement)this.MainGrid); Point ImageLocate = this.ImageArea.TransformToAncestor((UIElement)this.MainGrid).Transform(new Point(0, 0)); if (ImageLocate.X <= MousePoint.X && MousePoint.X <= ImageLocate.X + this.ImageArea.ActualWidth && ImageLocate.Y <= MousePoint.Y && MousePoint.Y <= ImageLocate.Y + this.ImageArea.ActualHeight) { this.Cursor = Cursors.Hand; } else { this.Cursor = Cursors.Arrow; } //边框拉伸 if (this.Action == MouseActionEx.Drag) { this.Cursor = this.MouseCursor; //剪辑图片区域宽高 double ImageAreaWidth = this.ImageArea.ActualWidth; double ImageAreaHeight = this.ImageArea.ActualHeight; //裁剪区域理论位置 RectangleAreaModel Model = this.CalculatedArea(MousePoint, false); if (Model != null) { //不能超出边界区域 if (Model.X + Model.Width + MaxMargin > this.ActualWidth || Model.Y + Model.Height + MaxMargin > this.ActualHeight || Model.X < MaxMargin || Model.Y < MaxMargin ) { this.Cursor = Cursors.Arrow; this.Action = MouseActionEx.None; return; } if (Model.Width < 0 || Model.Height < 0) { this.Cursor = Cursors.Arrow; this.Action = MouseActionEx.None; return; } this.ImageArea.Width = Model.Width; this.ImageArea.Height = Model.Height; this.ImageArea.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Left); this.ImageArea.SetValue(VerticalAlignmentProperty, VerticalAlignment.Top); this.ImageArea.SetValue(MarginProperty, new Thickness(Model.X, Model.Y, 0, 0)); // CutImage(); } } else if (this.Action == MouseActionEx.DragMove)//拖动 { double Left = this.MouseDownLocate.X + (MousePoint.X - MouseDownPoint.X); double Top = this.MouseDownLocate.Y + (MousePoint.Y - MouseDownPoint.Y); //不能超出边界区域 if (Left < MaxMargin || Top < MaxMargin || (Left + this.ImageArea.ActualWidth + MaxMargin) > this.ActualWidth || (Top + this.ImageArea.ActualHeight + MaxMargin) > this.ActualHeight) { this.Cursor = Cursors.Arrow; this.Action = MouseActionEx.None; return; } this.ImageArea.Width = this.ImageArea.ActualWidth; this.ImageArea.Height = this.ImageArea.ActualHeight; this.ImageArea.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Left); this.ImageArea.SetValue(VerticalAlignmentProperty, VerticalAlignment.Top); this.ImageArea.SetValue(MarginProperty, new Thickness(Left, Top, 0, 0)); //CutImage(); } else { //this.Cursor = Cursors.Arrow; } }