我是新来的WPF。
我想在画布上画一个圆圈在鼠标移动事件上。我已经写了逻辑把它拖到画布上。但是当鼠标点击我的画布时,我想要创建一个圆圈,它应该根据鼠标在画布上的移动来调整大小。
我怎样才能做到这一点?
这是我的密码
    Ellipse elip = new Ellipse();
    private double pointx;
    private double pointy;        
    private void canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        canvas.MouseMove -= MouseMove_NotDown;
        canvas.MouseMove += canvas_PreviewMouseMove;
        canvas.MouseUp += canvas_PreviewMouseUp;
        Point location = e.MouseDevice.GetPosition(canvas);
        elip = new Ellipse();
        elip.Height = 1;
        elip.Width = 1;
        elip.Stroke = Brushes.Black;
        elip.StrokeThickness = 2;
        Canvas.SetTop(elip, location.Y + 1);
        Canvas.SetLeft(elip, location.X + 1);
        pointx = location.X + 1;
        pointy = location.Y + 1;
        canvas.Children.Add(elip);
    }
    private void MouseMove_NotDown(object sender, MouseEventArgs e)
    {
        canvas.Cursor = Cursors.Hand;
    }
    private void canvas_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            Point location = e.MouseDevice.GetPosition(canvas);
            double height = location.Y - pointy;
            double width = location.X - pointx;
            if (height >= 0 && width >= 0)
            {
                elip.Height = height;
                elip.Width = width;
            }
            else if (height < 0 || width < 0)
            {
                if (height < 0)
                {
                    elip.Height = height + (-height) + 1;
                    elip.Width = width;
                }
                else if (width < 0)
                {
                    elip.Height = height;
                    elip.Width = width + (-width) + 1;
                }
            }
        }
        catch
        {
        }
    }
    private void canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        elip.Stroke = Brushes.Black;
        elip.StrokeThickness = 2;
        canvas.MouseMove -= canvas_PreviewMouseMove;
        canvas.MouseMove += MouseMove_NotDown;
        canvas.MouseUp += canvas_PreviewMouseUp;
    }发布于 2017-11-21 08:48:02
最好只附加你的听众一次。这使得您的应用程序逻辑更易于理解和调试。
如果WPF的canvas没有背景集,它将不会捕捉鼠标事件,除非在画布中有什么东西被点击,所以给它一个背景颜色(白色或透明是好的)
XAML:
<Window x:Class="mausing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:mausing"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas x:Name="canvas" Background="White"></Canvas>
</Grid>
C#:
public partial class MainWindow : Window
{
    private Ellipse elip = new Ellipse();
    private Point anchorPoint;
    public MainWindow()
    {
        InitializeComponent();
        canvas.MouseMove += canvas_MouseMove;
        canvas.MouseUp += canvas_MouseUp;
        canvas.MouseDown += canvas_MouseDown;
    }
    private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
    {
        //capture the mouse on the canvas
        //(this also helps us keep track of whether or not we're drawing)
        canvas.CaptureMouse();
        anchorPoint = e.MouseDevice.GetPosition(canvas);
        elip = new Ellipse
        {
            Stroke = Brushes.Black,
            StrokeThickness = 2
        };
        canvas.Children.Add(elip);
    }
    private void canvas_MouseMove(object sender, MouseEventArgs e)
    {
        //if we are not drawing, we don't need to do anything when the mouse moves
        if (!canvas.IsMouseCaptured)
            return;
        Point location = e.MouseDevice.GetPosition(canvas);
        double minX = Math.Min(location.X, anchorPoint.X);
        double minY = Math.Min(location.Y, anchorPoint.Y);
        double maxX = Math.Max(location.X, anchorPoint.X);
        double maxY = Math.Max(location.Y, anchorPoint.Y);
        Canvas.SetTop(elip, minY);
        Canvas.SetLeft(elip, minX);
        double height = maxY - minY;
        double width = maxX - minX;
        elip.Height = Math.Abs(height);
        elip.Width = Math.Abs(width);       
    }
    private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
    {
        // we are now no longer drawing
        canvas.ReleaseMouseCapture();
    }
}https://stackoverflow.com/questions/47407820
复制相似问题