我该如何在WPF中移动由按钮创建的形状?

4

我很新于C#和WPF,想要创建一个WPF应用程序,用按钮绘制形状,在画布上绕着移动。当我在XAML中创建一个形状时,它可以移动。但是我无法让通过按钮创建的形状移动。请问有人能帮忙吗?以下是我正在使用的XAML和代码:

XAML:

<Window x:Class="All_test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="canvas" >
    <Button Content="Button" Canvas.Left="250" Canvas.Top="260" Width="75" Click="Button_Click_1" />
    <Rectangle x:Name="rect" 
               Height="100" Width ="100" Fill="red"
               MouseLeftButtonDown="rect_MouseLeftButtonDown" 
            MouseLeftButtonUp="rect_MouseLeftButtonUp"
            MouseMove="rect_MouseMove" 
               Canvas.Left="342" Canvas.Top="110" />
</Canvas>

这是我用来移动在XAML中绘制的红色正方形的代码。如何对按钮创建的绿色正方形执行相同操作?

public partial class MainWindow : Window
{
    private bool _isRectDragInProg;

    public MainWindow()
    {
        InitializeComponent();
    }


    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Rectangle rect = new Rectangle();
        rect.Fill = new SolidColorBrush(Colors.Green);
        rect.Stroke = new SolidColorBrush(Colors.Black);
        rect.Height = 100;
        rect.Width = 100;
        rect.StrokeThickness = 4;
        canvas.Children.Add(rect);
        InitializeComponent();

    }


    private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _isRectDragInProg = true;
        rect.CaptureMouse();
    }


    private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _isRectDragInProg = false;
        rect.ReleaseMouseCapture();
    }


    private void rect_MouseMove(object sender, MouseEventArgs e)
    {
        if (!_isRectDragInProg) return;

        // get the position of the mouse relative to the Canvas
        var mousePos = e.GetPosition(canvas);

        // center the rect on the mouse
        double left = mousePos.X - (rect.ActualWidth / 2);
        double top = mousePos.Y - (rect.ActualHeight / 2);
        Canvas.SetLeft(rect, left);
        Canvas.SetTop(rect, top);
    }
1个回答

3

您需要为此Rectangle绑定鼠标事件:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    Rectangle rect = new Rectangle();
    rect.Fill = new SolidColorBrush(Colors.Green);
    rect.Stroke = new SolidColorBrush(Colors.Black);
    rect.Height = 100;
    rect.Width = 100;
    rect.StrokeThickness = 4;
    // here
    rect.MouseLeftButtonDown += rect_MouseLeftButtonDown;
    rect.MouseLeftButtonUp += rect_MouseLeftButtonUp;
    rect.MouseMove += rect_MouseMove;

    canvas.Children.Add(rect);
    // InitializeComponent();   <--- lose the InitializeComponent here, that's should only be called ones.. (in the constructor)
}

建议:

  • 使用sender参数获取当前Rectangle
  • 您可以放弃_isRectDragInProg布尔值……改用IsMouseCaptured属性。

例如:

private void rect_MouseMove(object sender, MouseEventArgs e)
{
    var rect = (Rectangle)sender;

    if (!rect.IsMouseCaptured) return;

    // get the position of the mouse relative to the Canvas
    var mousePos = e.GetPosition(canvas);

    // center the rect on the mouse
    double left = mousePos.X - (rect.ActualWidth / 2);
    double top = mousePos.Y - (rect.ActualHeight / 2);
    Canvas.SetLeft(rect, left);
    Canvas.SetTop(rect, top);
}

谢谢,先生!虽然需要一些调整,但总体上这帮助我理解了这个想法。也感谢您的建议。 :) - Greengored

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接