//xaml <Window x:Class="WpfApp11.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:WpfApp11" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid> <Canvas Background="White" Name="cvs" MouseLeftButtonDown="cvs_MouseLeftButtonDown" MouseMove="cvs_MouseMove" MouseUp="cvs_MouseUp"/> </Grid> </Window> //xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp11 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { Point _pos; bool _isDrawing; Brush _stroke=Brushes.Black; public MainWindow() { InitializeComponent(); } private void cvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _isDrawing = true; _pos = e.GetPosition(cvs); cvs.CaptureMouse(); } private void cvs_MouseMove(object sender, MouseEventArgs e) { if(_isDrawing) { Line line = new Line(); line.X1 = _pos.X; line.Y1 = _pos.Y; _pos=e.GetPosition(cvs); line.X2 = _pos.X; line.Y2= _pos.Y; line.Stroke = _stroke; line.StrokeThickness = 10; cvs.Children.Add(line); } } private void cvs_MouseUp(object sender, MouseButtonEventArgs e) { _isDrawing=false; cvs.ReleaseMouseCapture(); } } }
标签:draw,via,Windows,lines,System,cvs,pos,using,line From: https://www.cnblogs.com/Fred1987/p/18091895