C# 如何在画布上绘制一个可拖动的矩形?
我有这三个函数来触发事件。我已经有了一个静态版本的需求,但我需要一个动态版本的需求 我已经在WPF中制作了一个名为“canvasPreview”的画布,我想将矩形(目前在静态版本中,我使用的是椭圆)添加到画布中,它必须可以通过上述函数拖动。它已经在工作了,但必须是动态的C# 如何在画布上绘制一个可拖动的矩形?,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我有这三个函数来触发事件。我已经有了一个静态版本的需求,但我需要一个动态版本的需求 我已经在WPF中制作了一个名为“canvasPreview”的画布,我想将矩形(目前在静态版本中,我使用的是椭圆)添加到画布中,它必须可以通过上述函数拖动。它已经在工作了,但必须是动态的 我希望你能帮助我,提前谢谢你 我相信此示例代码将对您有所帮助 XAML: 如果不是动态的,它是如何工作的?你说它必须是动态的到底是什么意思?这个形状不会被拖走吗?对不起。静态版本正在工作,我的意思是,在WPF中创建的椭圆是可拖动
我希望你能帮助我,提前谢谢你 我相信此示例代码将对您有所帮助 XAML:
如果不是动态的,它是如何工作的?你说它必须是动态的到底是什么意思?这个形状不会被拖走吗?对不起。静态版本正在工作,我的意思是,在WPF中创建的椭圆是可拖动的,但是当我尝试生成椭圆(应该是矩形)时,它不可拖动,但是调用了函数。函数在画布中吗?如果插入断点并开始尝试拖动矩形,“源”在MouseLeftButtonDown中设置为什么?MouseDragElementBehavior呢?来自混合交互行为?[谢谢!问题解决了!是SetLeft和SetTop属性。祝您好运,祝您成功。
bool captured = false;
double x_shape, x_canvas, y_shape, y_canvas;
UIElement source = null;
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
source = (UIElement)sender;
Mouse.Capture(source);
captured = true;
x_shape = Canvas.GetLeft(source);
x_canvas = e.GetPosition(canvasPreview).X;
y_shape = Canvas.GetTop(source);
y_canvas = e.GetPosition(canvasPreview).Y;
}
private void MouseMove(object sender, MouseEventArgs e)
{
//MessageBox.Show("test");
if (captured)
{
double x = e.GetPosition(canvasPreview).X;
double y = e.GetPosition(canvasPreview).Y;
x_shape += x - x_canvas;
Canvas.SetLeft(source, x_shape);
x_canvas = x;
y_shape += y - y_canvas;
Canvas.SetTop(source, y_shape);
y_canvas = y;
}
}
private void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
captured = false;
}
<Grid Margin="12">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button x:Name="addRectangleButton" Content="Add Rectngle" Click="addRectangleButton_Click"/>
</StackPanel>
<Canvas Grid.Row="1" x:Name="canvas" Margin="0,12,0,0">
<Rectangle x:Name="rectangle" Width="100" Height="50" Fill="RoyalBlue" MouseDown="rectangle_MouseDown" MouseMove="rectangle_MouseMove" MouseUp="rectangle_MouseUp" Canvas.Left="0" Canvas.Top="0"/>
</Canvas>
</Grid>
bool drag = false;
Point startPoint;
public MainWindow()
{
InitializeComponent();
}
// this creates and adds rectangles dynamically
private void addRectangleButton_Click(object sender, RoutedEventArgs e)
{
// create new Rectangle
Rectangle rectangle = new Rectangle();
// assign properties
rectangle.Width = 100;
rectangle.Height = 50;
rectangle.Fill = new SolidColorBrush(Colors.RoyalBlue);
// assign handlers
rectangle.MouseDown += rectangle_MouseDown;
rectangle.MouseMove += rectangle_MouseMove;
rectangle.MouseUp += rectangle_MouseUp;
// set default position
Canvas.SetLeft(rectangle, 0);
Canvas.SetTop(rectangle, 0);
// add it to canvas
canvas.Children.Add(rectangle);
}
private void rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
// start dragging
drag = true;
// save start point of dragging
startPoint = Mouse.GetPosition(canvas);
}
private void rectangle_MouseMove(object sender, MouseEventArgs e)
{
// if dragging, then adjust rectangle position based on mouse movement
if (drag)
{
Rectangle draggedRectangle = sender as Rectangle;
Point newPoint = Mouse.GetPosition(canvas);
double left = Canvas.GetLeft(draggedRectangle);
double top = Canvas.GetTop(draggedRectangle);
Canvas.SetLeft(draggedRectangle, left + (newPoint.X - startPoint.X));
Canvas.SetTop(draggedRectangle, top + (newPoint.Y - startPoint.Y));
startPoint = newPoint;
}
}
private void rectangle_MouseUp(object sender, MouseButtonEventArgs e)
{
// stop dragging
drag = false;
}