using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
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 _109_移动Ellipse
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Point arcsP = new Point();
Point arceP = new Point();
Point arcM42P = new Point();
Point arcM43P = new Point();
int sel2 = 0;
Stopwatch sw1 = new Stopwatch();
private void Canv1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource == Canv1)
{
sel2 = 0;
if (sel2 == 0)
{
Canv1.Children.Clear();
arcsP = e.GetPosition(Canv1);
sel2 = 1;
}
}
}
private void Canv1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (sel2 == 1)
{
CalPath(e);
sel2 = 2;
}
}
List<Ellipse> drawE = new List<Ellipse>();
private void Canv1_PreviewMouseMove(object sender, MouseEventArgs e)
{
sw1.Start();
if (e.OriginalSource == Canv1)
{
if (sel2 == 1 && e.LeftButton == MouseButtonState.Pressed)
{
CalPath(e);
}
}
else if (drawE.Count > 3 && sel2 == 2)
{
if (e.OriginalSource == drawE[1] && e.LeftButton == MouseButtonState.Pressed)
{
CalPath(drawE[1],1,e);
}
if (e.OriginalSource == drawE[2] && e.LeftButton == MouseButtonState.Pressed)
{
CalPath(drawE[2], 2, e);
}
}
sw1.Stop();
spanT.Content = sw1.Elapsed;
sw1.Reset();
}
private void CalPath(MouseEventArgs e)
{
ObservableCollection<Point> ItemsSource = new ObservableCollection<Point>();
Canv1.Children.Clear();
arceP = e.GetPosition(Canv1);
double esPX = arceP.X - arcsP.X;
double esPY = arceP.Y - arcsP.Y;
arcM42P.X = arcsP.X + esPX / 6 * 2;
arcM42P.Y = arcsP.Y + esPY / 6 * 2 + 15;
arcM43P.X = arcsP.X + esPX / 6 * 4;
arcM43P.Y = arcsP.Y + esPY / 6 * 4 - 15;
ItemsSource.Add(arcsP);
ItemsSource.Add(arcM42P);
ItemsSource.Add(arcM43P);
ItemsSource.Add(arceP);
DrawPath(ItemsSource);
DrawEllipse(ItemsSource);
}
private void DrawPath(ObservableCollection<Point> ItemsSource)
{
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = ItemsSource[0];
pathFigure.Segments.Add(new BezierSegment(ItemsSource[1], ItemsSource[2], ItemsSource[3], true));
PathFigureCollection pfc = new PathFigureCollection();
pfc.Add(pathFigure);
PathGeometry pg = new PathGeometry();
pg.Figures = pfc;
Path path = new Path();
path.Data = pg;
path.Stroke = Brushes.Green;
path.StrokeThickness = 2;
Canv1.Children.Add(path);
}
int name = 0;
double h = 20, w = 20;
private void DrawEllipse(ObservableCollection<Point> itemsSource)
{
ObservableCollection<Ellipse> es = new ObservableCollection<Ellipse>();
drawE.Clear();
for (int i = 0; i < itemsSource.Count; i++)
{
Ellipse TempE = new Ellipse()
{
Name = "Ellipse" + name,
Height = h,
Width = w,
Fill = Brushes.Red,
Margin = new Thickness()
{
Left = itemsSource[i].X - w / 2,
Top = itemsSource[i].Y - h / 2,
Right = Canv1.Width - w - itemsSource[i].X,
Bottom = Canv1.Height - h - itemsSource[i].Y
}
};
Canv1.Children.Add(TempE);
drawE.Add(TempE);
name++;
}
}
private void CalPath(Ellipse ep, int CtrlP, MouseEventArgs e)
{
Point newP = e.GetPosition(Canv1);
ep.Margin = new Thickness()
{
Left = newP.X - w / 2,
Top = newP.Y - h / 2,
Right = Canv1.Width - w - newP.X,
Bottom = Canv1.Height - h - newP.Y
};
switch (CtrlP)
{
case 1:
arcM42P = newP;
break;
case 2:
arcM43P = newP;
break;
}
ObservableCollection<Point> ItemsSource = new ObservableCollection<Point>();
ItemsSource.Add(arcsP);
ItemsSource.Add(arcM42P);
ItemsSource.Add(arcM43P);
ItemsSource.Add(arceP);
ModPath(drawE, ItemsSource);
}
private void ModPath(List<Ellipse> ellipses, ObservableCollection<Point> itemsSource)
{
Canv1.Children.Clear();
for (int i = 0; i < ellipses.Count; i++)
{
Canv1.Children.Add(ellipses[i]);
}
for (int i = 0; i < itemsSource.Count; i++)
{
DrawPath(itemsSource);
}
}
}
}
<Window x:Class="_109_移动Ellipse.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:_109_移动Ellipse"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Name="mainGrid">
<Grid Name="ChildGrid1" Background="LightBlue" Margin="0,0,421,0" >
<Label Name="spanT" Content="0" Height="50" Width="200" Margin="88,94,83,275"></Label>
</Grid>
<Canvas Name="Canv1" Margin="379,0,0,0" Background="LightGray"
PreviewMouseLeftButtonDown="Canv1_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="Canv1_PreviewMouseLeftButtonUp"
PreviewMouseMove="Canv1_PreviewMouseMove"
Height="410" Width="410"
></Canvas>
</Grid>
</Window>
WPF绘制拖拽曲线和移动控制点修改路径
最新推荐文章于 2024-02-04 16:21:06 发布