C# WPF画线

在WPF中实现的效果如下:

 

 

WinFrom画线参考:C# Winform画线 - 十年新 - 博客园 (cnblogs.com)

代码请参考:

XAML:

 1 <Window x:Class="LineDemo.MainWindow"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6     xmlns:local="clr-namespace:LineDemo"
 7     mc:Ignorable="d"
 8     Title="MainWindow" Height="450" Width="800">
 9     <Grid>
10         <Line x:Name="RedLine" Stroke="Purple" StrokeThickness="2" Visibility="Hidden" StrokeDashArray="3 3" ></Line>
11         <Grid>
12             <Grid.RowDefinitions>
13                 <RowDefinition Height="9*"></RowDefinition>
14                 <RowDefinition Height="1*"></RowDefinition>
15             </Grid.RowDefinitions>
16             <Grid></Grid>
17             <Grid Grid.Row="1">
18                 <Label VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Calibri" 
19                        FontSize="14" FontWeight="Bold" FontStyle="Italic" Foreground="Red">https://www.cnblogs.com/yellow3gold/</Label>
20             </Grid>
21         </Grid>
22     </Grid>
23 </Window>

.cs

 1 using System.Windows;
 2 using System.Windows.Input;
 3 
 4 namespace LineDemo
 5 {
 6     /// <summary>
 7     /// MainWindow.xaml 的交互逻辑
 8     /// </summary>
 9     public partial class MainWindow : Window
10     {
11         public MainWindow()
12         {
13             InitializeComponent();
14             this.MouseMove += MainWindow_MouseMove;
15             this.MouseDown += MainWindow_MouseDown;
16         }
17 
18         private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
19         {
20             if (_isStart)
21             {
22                 Point endPoint = e.MouseDevice.GetPosition(this);
23                 RedLine.X1 = _startPoint.X;
24                 RedLine.Y1 = _startPoint.Y;
25                 RedLine.X2 = endPoint.X;
26                 RedLine.Y2 = endPoint.Y;
27                 RedLine.Visibility = Visibility.Visible;
28                 _isStart = false;
29             }
30             else
31             {
32                 _startPoint = e.MouseDevice.GetPosition(this);
33                 RedLine.Visibility = Visibility.Hidden;
34                 _isStart = true;
35             }
36         }
37 
38         private bool _isStart;
39         private Point _startPoint;
40 
41         public void MainWindow_MouseMove(object sender, MouseEventArgs e)
42         {
43             var p = e.MouseDevice.GetPosition(this);
44             this.Title = "Location:X=" + p.X + ",Y=" + p.Y;
45             if (!_isStart || _startPoint.X == 0) return;
46             RedLine.X1 = _startPoint.X;
47             RedLine.Y1 = _startPoint.Y;
48             RedLine.X2 = p.X;
49             RedLine.Y2 = p.Y;
50             RedLine.Visibility = Visibility.Visible;
51         }
52     }
53 }
posted @ 2022-02-16 10:16  新*  阅读(1215)  评论(0编辑  收藏  举报