【问题标题】:How can I move shape created by a button on the canvas in WPF?如何移动由 WPF 画布上的按钮创建的形状?
【发布时间】:2016-05-24 09:34:05
【问题描述】:

我是 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);
    }

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    你应该为这个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);
    }
    

    【讨论】:

    • 谢谢你,好先生!需要进行一些调整,但总的来说,这帮助我掌握了这个想法。这些建议也受到赞赏。 :)
    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多