WPF绘制拖拽曲线和移动控制点修改路径

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的框架,可用于创建各种各样的图形用户界面(GUI),包括实时曲线图。 要实现实时曲线图中x轴的自动滚动,可以采取以下步骤: 1. 创建一个WPF应用程序并设置其主窗口。 2. 在XAML中定义一个Canvas元素,它将用于绘制实时曲线图。在Canvas上,您可以添加其他需要的元素,例如坐标轴等。 3. 在代码中,创建一个定时器(例如使用System.Timers.Timer类),以固定的时间间隔触发更新曲线图的操作。 4. 在定时器的Tick事件处理程序中,更新实时曲线图的数据,并重新绘制曲线图。 5. 在绘制曲线图时,可以通过调整x轴的坐标值来实现自动滚动的效果。例如,可以记录最后一个数据点的x轴坐标值,并将所有数据点的x轴坐标值减去该值,以实现滚动效果。 6. 然后,根据新的x轴坐标值重新绘制曲线图。 以下是一个简单示例代码: ```csharp private System.Timers.Timer timer; private double xOffset = 0; public MainWindow() { InitializeComponent(); timer = new System.Timers.Timer(100); // 设置定时器间隔为100毫秒 timer.Elapsed += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, ElapsedEventArgs e) { // 更新曲线图数据 // 调整x轴坐标值 double lastX = GetLastDataPointXValue(); xOffset += lastX; // 重新绘制曲线图 Dispatcher.BeginInvoke(new Action(() => { DrawCurve(); })); } private void DrawCurve() { // 清除Canvas上的所有图形元素 // 根据新的x轴坐标值重新绘制曲线图 double xOffset = 0; foreach (DataPoint dataPoint in dataPoints) { // 计算新的x轴坐标值 double newX = dataPoint.X - xOffset; // 绘制曲线图上的数据点 // 更新x轴坐标值为最新的值 dataPoint.X = newX; } } ``` 这样,每当定时器触发时,曲线图的x轴坐标值都会根据最新的数据进行调整,从而实现自动滚动的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值