wpf mvvmlight 实现鼠标拖拽改变rectangle大小以及鼠标操作控制旋转
时间: 2024-03-08 16:51:02 浏览: 124
MvvmLight_WPF_Frame_Nav:具有框架和页面的MvvmLight WPF导航
要实现鼠标拖拽改变Rectangle大小以及鼠标操作控制旋转,可以使用WPF的MVVMLight框架来实现。
首先,在ViewModel中定义Rectangle的位置、大小和角度等属性,以及鼠标拖拽和鼠标操作的事件处理函数。
```
public class MainViewModel : ViewModelBase
{
private double _left;
private double _top;
private double _width;
private double _height;
private double _angle;
private bool _isDragging;
private bool _isRotating;
private Point _lastMousePosition;
public double Left
{
get { return _left; }
set { Set(ref _left, value); }
}
public double Top
{
get { return _top; }
set { Set(ref _top, value); }
}
public double Width
{
get { return _width; }
set { Set(ref _width, value); }
}
public double Height
{
get { return _height; }
set { Set(ref _height, value); }
}
public double Angle
{
get { return _angle; }
set { Set(ref _angle, value); }
}
public void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
_lastMousePosition = e.GetPosition(Application.Current.MainWindow);
if (e.OriginalSource is FrameworkElement element)
{
// 判断鼠标操作类型
if (element.Name == "ResizeThumb")
{
_isDragging = false;
_isRotating = false;
}
else if (element.Name == "RotateThumb")
{
_isDragging = false;
_isRotating = true;
}
else
{
_isDragging = true;
_isRotating = false;
}
}
}
}
public void OnMouseMove(object sender, MouseEventArgs e)
{
if (_isDragging)
{
// 鼠标拖拽改变Rectangle大小
}
else if (_isRotating)
{
// 鼠标操作控制Rectangle旋转
}
}
public void OnMouseUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
_isRotating = false;
}
}
```
接着,在View中使用MVVMLight框架提供的Binding绑定属性和事件处理函数。
```
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Grid>
<Rectangle Fill="Blue" Width="{Binding Width}" Height="{Binding Height}"
Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}"
RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="{Binding Angle}" />
</Rectangle.RenderTransform>
<Thumb x:Name="ResizeThumb" Width="10" Height="10" Cursor="SizeNWSE"
DragDelta="{Binding ResizeThumb_DragDelta}" />
<Thumb x:Name="RotateThumb" Width="10" Height="10" Cursor="SizeAll"
Canvas.Left="{Binding Width, Converter={StaticResource AngleToXConverter}}"
Canvas.Top="{Binding Height, Converter={StaticResource AngleToYConverter}}"
DragDelta="{Binding RotateThumb_DragDelta}" />
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<i:InvokeCommandAction Command="{Binding MouseDownCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction Command="{Binding MouseMoveCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<i:InvokeCommandAction Command="{Binding MouseUpCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</Grid>
```
最后,在ViewModel中实现鼠标拖拽和鼠标操作事件处理函数,计算Rectangle的位置、大小和角度。
```
public RelayCommand<DragDeltaEventArgs> ResizeThumb_DragDelta { get; private set; }
public RelayCommand<DragDeltaEventArgs> RotateThumb_DragDelta { get; private set; }
public MainViewModel()
{
ResizeThumb_DragDelta = new RelayCommand<DragDeltaEventArgs>(OnResizeThumbDragDelta);
RotateThumb_DragDelta = new RelayCommand<DragDeltaEventArgs>(OnRotateThumbDragDelta);
}
public void OnResizeThumbDragDelta(DragDeltaEventArgs e)
{
Width = Math.Max(0, Width + e.HorizontalChange);
Height = Math.Max(0, Height + e.VerticalChange);
}
public void OnRotateThumbDragDelta(DragDeltaEventArgs e)
{
var center = new Point(Width / 2, Height / 2);
var currentMousePosition = e.GetPosition(Application.Current.MainWindow);
var delta = currentMousePosition - _lastMousePosition;
_lastMousePosition = currentMousePosition;
var angle = Math.Atan2(delta.Y, delta.X) * 180 / Math.PI;
Angle = (Angle + angle) % 360;
RotateThumbUpdatePosition(center);
}
private void RotateThumbUpdatePosition(Point center)
{
var angleRad = Angle * Math.PI / 180.0;
var x = center.X + (Width / 2) * Math.Cos(angleRad) + (Height / 2) * Math.Sin(angleRad);
var y = center.Y - (Width / 2) * Math.Sin(angleRad) + (Height / 2) * Math.Cos(angleRad);
Canvas.SetLeft(RotateThumb, x - RotateThumb.ActualWidth / 2);
Canvas.SetTop(RotateThumb, y - RotateThumb.ActualHeight / 2);
}
public void OnMouseMove(MouseEventArgs e)
{
if (_isDragging)
{
var currentMousePosition = e.GetPosition(Application.Current.MainWindow);
var delta = currentMousePosition - _lastMousePosition;
_lastMousePosition = currentMousePosition;
Left = Math.Max(0, Left + delta.X);
Top = Math.Max(0, Top + delta.Y);
}
else if (_isRotating)
{
var center = new Point(Width / 2, Height / 2);
var currentMousePosition = e.GetPosition(Application.Current.MainWindow);
var delta = currentMousePosition - _lastMousePosition;
_lastMousePosition = currentMousePosition;
var angle = Math.Atan2(delta.Y, delta.X) * 180 / Math.PI;
Angle = (Angle + angle) % 360;
RotateThumbUpdatePosition(center);
}
}
public void OnMouseUp(MouseButtonEventArgs e)
{
_isDragging = false;
_isRotating = false;
}
```
这样就可以通过MVVMLight框架实现鼠标拖拽改变Rectangle大小以及鼠标操作控制旋转了。
阅读全文