后台代码
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;
using Microsoft.Expression.Shapes;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private Boolean isdraw = false;
Point[] p = new Point[2];
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
isdraw = true;
}
private void InkCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed&&isdraw)
{
p[0] = e.GetPosition(canvas);
}
}
private void InkCanvas_MouseUp(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released&&isdraw)
{
p[1] = e.GetPosition(canvas);
BlockArrow b = new BlockArrow();
b.Fill = Brushes.Red;
this.canvas.Children.Add(b);
b.Margin = new Thickness(p[0].X, p[0].Y, canvas.ActualWidth - p[0].X, canvas.ActualHeight - p[0].Y);
double w = Math.Abs(p[0].X - p[1].X);
double h = Math.Abs(p[0].Y - p[1].Y);
b.Width = Math.Sqrt(w * w + h * h);
b.Height = 10;
b.RenderTransform = new RotateTransform(Math.Atan2((p[1].Y - p[0].Y), (p[1].X - p[0].X))*180/Math.PI);
isdraw = false;
}
}
}
}
前台代码
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<InkCanvas Name="canvas" Grid.Row="0" PreviewMouseDown="InkCanvas_MouseDown" MouseUp="InkCanvas_MouseUp">
</InkCanvas>
<Button Grid.Row="1" Content="画箭头" Click="Button_Click">
</Button>
</Grid>
</Window>
写了一个DEMO 你看看 这只是一个思路 这个DEMO没有屏蔽画箭头时候的自带划线 也没有预览功能 箭头是一个现成的对象 你可以看看这样的思路是否可以
很有帮助,谢谢!!!!