我是靠谱客的博主 魁梧未来,最近开发中收集的这篇文章主要介绍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!");
        }
        
    }
    
}

最后

以上就是魁梧未来为你收集整理的wpf-鼠标点击事件-单击左键画连续线段点击右键停止的全部内容,希望文章能够帮你解决wpf-鼠标点击事件-单击左键画连续线段点击右键停止所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部