wpf-鼠标点击事件-单击左键画连续线段点击右键停止

问题描述

希望在图片上画一些头尾相连的线段。单击左键绘图开始,单击右键绘图停止。如下图所示。
在这里插入图片描述

解决方案

前台

<Window x:Class="TestAvalon.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:TestAvalon"
        xmlns:avalonDock="http://schemas.xceed.com/wpf/xaml/avalondock"
        mc:Ignorable="d"
        d:DesignHeight="600" d:DesignWidth="700"
        >
    <Grid x:Name="grid" MouseLeftButtonDown="grid_MouseDown" MouseRightButtonDown="imgBox_MouseRightButtonDown">
        <Image Name="imgBox" Height="500" Width="600" Source="pack://application:,,,/images/Smile.png"
                RenderOptions.BitmapScalingMode="NearestNeighbor" 
               HorizontalAlignment="Center"
               VerticalAlignment="Center" MouseMove="imgBox_MouseMove" 
               >
            <Image.RenderTransform>
                <ScaleTransform x:Name="ScaleTran" />
            </Image.RenderTransform>
        </Image>
        <Canvas Name="Imgcanvas" Height="500" Width="600">
            <Polyline Stroke="Red" StrokeThickness="1" Name="lines"></Polyline>
            <Path Stroke="Red">
                <Path.Data>
                    <PathGeometry>
                        <PathFigure x:Name="pathImg">
                            <LineSegment x:Name="lineImg"/>
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </Canvas>
    </Grid>
</Window>

后台

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace TestAvalon
{
    
    
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    
    
        public bool captureMode {
    
     get; set; }
        public Point lineStartPoint {
    
     get; set; }
        public PointCollection pointsCollection {
    
     get; set; }
        
        public MainWindow()
        {
    
    
            InitializeComponent();
            lineStartPoint = new Point();
            pointsCollection = new PointCollection();
        }
        
        private void imgBox_MouseMove(object sender, MouseEventArgs e)
        {
    
    
            if (captureMode)
            {
    
    
                pathImg.StartPoint = lineStartPoint;
                lineImg.Point = e.GetPosition(Imgcanvas);
            }
        }
        
        private void grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
    
    
            captureMode = false;
            Point point = new Point();
            point = e.GetPosition(Imgcanvas);
            lineStartPoint = point;
            pointsCollection.Add(point);
            lines.Points = pointsCollection;
            captureMode = true;
        }
        
        private void imgBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
    
    
            foreach (Point p in pointsCollection) {
    
    
                Console.WriteLine("x:{0} y:{1}", p.X, p.Y);
                Console.WriteLine("****************");
            }
            captureMode = false;
            lineStartPoint = new Point();
            pointsCollection = new PointCollection();
            MessageBox.Show("done!");
        }
        
    }
    
}

猜你喜欢

转载自blog.csdn.net/pxy7896/article/details/118484546
今日推荐