繁体   English   中英

WPF如何在鼠标事件上绘制圆

[英]WPF How to draw Circle on Mouse events

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

最好只将一次听众连接一次。 这使您的应用程序逻辑更易于理解和调试。

如果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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM