1 //xaml 2 <Window x:Class="WpfApp67.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 WindowState="Maximized" 8 xmlns:local="clr-namespace:WpfApp67" 9 mc:Ignorable="d" 10 Title="MainWindow" Height="450" Width="800"> 11 <Grid> 12 <Grid.RowDefinitions> 13 <RowDefinition/> 14 <RowDefinition Height="50"/> 15 </Grid.RowDefinitions> 16 <Canvas x:Name="cvs" 17 Grid.Row="0" 18 Background="Transparent" 19 PreviewMouseDown="cvs_PreviewMouseDown" 20 PreviewMouseUp="cvs_PreviewMouseUp" 21 PreviewMouseMove="cvs_PreviewMouseMove"> 22 <!--<Rectangle x:Name="rect" 23 Visibility="Collapsed" 24 Fill="LightBlue" 25 Stroke="DarkBlue" 26 StrokeThickness="5"> 27 </Rectangle>--> 28 </Canvas> 29 <StatusBar Grid.Row="1"> 30 <StatusBarItem> 31 <TextBlock x:Name="statusTbk" 32 FontSize="20"/> 33 </StatusBarItem> 34 </StatusBar> 35 </Grid> 36 </Window> 37 38 39 40 41 //cs 42 using System; 43 using System.Collections.Generic; 44 using System.Linq; 45 using System.Text; 46 using System.Threading.Tasks; 47 using System.Windows; 48 using System.Windows.Controls; 49 using System.Windows.Data; 50 using System.Windows.Documents; 51 using System.Windows.Input; 52 using System.Windows.Media; 53 using System.Windows.Media.Imaging; 54 using System.Windows.Navigation; 55 using System.Windows.Shapes; 56 57 namespace WpfApp67 58 { 59 /// <summary> 60 /// Interaction logic for MainWindow.xaml 61 /// </summary> 62 public partial class MainWindow : Window 63 { 64 private Point startPt, endPt; 65 string statusMsg = string.Empty; 66 int idx = 0; 67 List<Rectangle> rectsList = new List<Rectangle>(); 68 69 List<Line> linesList = new List<Line>(); 70 71 public MainWindow() 72 { 73 InitializeComponent(); 74 } 75 76 private void cvs_PreviewMouseDown(object sender, MouseButtonEventArgs e) 77 { 78 startPt = e.GetPosition(cvs); 79 rectsList.Add(new Rectangle()); 80 linesList.Add(new Line()); 81 } 82 83 private void cvs_PreviewMouseUp(object sender, MouseButtonEventArgs e) 84 { 85 if (!cvs.Children.Contains(rectsList[idx])) 86 { 87 rectsList[idx].ToolTip = $"Rect[{idx+1}],{Guid.NewGuid().ToString("N")}"; 88 cvs.Children.Add(rectsList[idx]); 89 } 90 if (!cvs.Children.Contains(linesList[idx])) 91 { 92 linesList[idx].ToolTip = $"Line[{idx + 1}],{Guid.NewGuid().ToString("N")}"; 93 cvs.Children.Add(linesList[idx]); 94 } 95 ++idx; 96 } 97 98 private void cvs_PreviewMouseMove(object sender, MouseEventArgs e) 99 { 100 if (Mouse.LeftButton == MouseButtonState.Pressed) 101 { 102 endPt = e.GetPosition(cvs); 103 Rectangle rect = rectsList[idx]; 104 rect.Stroke = new SolidColorBrush(Colors.DarkBlue); 105 rect.StrokeThickness = 5; 106 rect.Visibility = Visibility.Visible; 107 rect.Width = Math.Abs(endPt.X - startPt.X); 108 rect.Height = Math.Abs(endPt.Y - startPt.Y); 109 Canvas.SetLeft(rect, startPt.X); 110 Canvas.SetTop(rect, startPt.Y); 111 112 113 Line ln = linesList[idx]; 114 ln.X1 = startPt.X+2.5; 115 ln.Y1 = startPt.Y+2.5; 116 ln.X2 = endPt.X-2.5; 117 ln.Y2 = endPt.Y-2.5; 118 ln.Stroke = new SolidColorBrush(Colors.DarkBlue); 119 ln.StrokeThickness = 1; 120 121 statusMsg = $"Start:({startPt.X},{startPt.Y}),end point:({endPt.X},{endPt.Y})"; 122 UpdateStatus(statusMsg); 123 if (!cvs.Children.Contains(rectsList[idx])) 124 { 125 cvs.Children.Add(rectsList[idx]); 126 } 127 if (!cvs.Children.Contains(linesList[idx])) 128 { 129 cvs.Children.Add(linesList[idx]); 130 } 131 } 132 } 133 134 private void UpdateStatus(string msg) 135 { 136 Application.Current.Dispatcher.Invoke(new Action(() => 137 { 138 statusTbk.Text = msg; 139 })); 140 } 141 } 142 }
标签:draw,idx,Windows,cvs,System,startPt,using,line,WPF From: https://www.cnblogs.com/Fred1987/p/18608642