用鼠标在窗体上移动椭圆

问题描述 投票:4回答:2

如何在WPF中通过鼠标在窗口上移动椭圆。

private void ellipse_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null && e.LeftButton == MouseButtonState.Pressed)
    {
        DragDrop.DoDragDrop(ellipse,
                            ellipse.Fill.ToString(),
                            System.Windows.DragDropEffects.Copy);
    }
}

如何创建方法ellipse_MouseClick

c# wpf
2个回答
4
投票

Ellipses上没有MouseClick事件,但是有MouseDown和MouseUp事件。我假设你正在寻找这样的东西。

WPF:

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="350" Width="525"
        MouseMove="Any_MouseMove"
        >
    <Canvas>
        <Ellipse Fill="Lavender" Height="100" Width="100" 
                 MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 
                 MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 
                 MouseMove="Any_MouseMove" />
    </Canvas>
</Window>

代码背后:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        private UIElement _lastClickedUIElement;
        private Point? _clickOffset;

        public MainWindow() { InitializeComponent(); }

        private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _lastClickedUIElement = sender as UIElement;
            _clickOffset = e.GetPosition(_lastClickedUIElement);
        }

        private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _lastClickedUIElement = null;
        }

        private void Any_MouseMove(object sender, MouseEventArgs e)
        {
            if (_lastClickedUIElement == null)
                return;

            _lastClickedUIElement.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - _clickOffset.Value.X);
            _lastClickedUIElement.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - _clickOffset.Value.Y);
        }
    }
}

点击圆圈移动它。只要您为这些方法提供这些方法,这将适用于任何UI元素。随意添加一个矩形到画布。

<Rectangle Fill="Lavender" Height="100" Width="100" 
         MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 
         MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 
         MouseMove="Any_MouseMove" />

1
投票

我使用三个事件来完成这个任务:

XAML:

<Window x:Class="WpfPainting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="700" WindowStartupLocation="CenterScreen">
    <DockPanel>
        <Canvas Background="White" Name="CanvasArea"/>
    </DockPanel>
</Window>

码:

private UIElement source;
private bool captured;
double x_shape, x_canvas, y_shape, y_canvas;
private void ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Mouse.OverrideCursor = Cursors.SizeAll;
    source = (UIElement)sender;
    Mouse.Capture(source);
    captured = true;
    x_shape = Canvas.GetLeft(source);
    x_canvas = e.GetPosition(CanvasArea).X;
    y_shape = Canvas.GetTop(source);
    y_canvas = e.GetPosition(CanvasArea).Y;
}

private void ellipse_MouseMove(object sender, MouseEventArgs e)
{
    if (captured)
    {
        double x = e.GetPosition(CanvasArea).X;
        double y = e.GetPosition(CanvasArea).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 ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Mouse.Capture(null);
    captured = false;
    Mouse.OverrideCursor = null;
}

要在绘制的椭圆上使用事件,我会像这样引用它们:

ellipse.MouseLeftButtonDown += conveyor_MouseLeftButtonDown;
ellipse.MouseMove += conveyor_MouseMove;
ellipse.MouseLeftButtonUp += conveyor_MouseLeftButtonUp;
© www.soinside.com 2019 - 2024. All rights reserved.