在C#中绘制连接线

时间:2014-12-18 18:29:18

标签: c# animation storyboard

我设法为一个工作中的小项目清除一些可行的C#代码,但我遇到了一个问题。此代码将在正在播放的视频上绘制一条线,但我需要对其进行修改,以便第二行将在原始行的末尾开始绘制,以便我可以通过连接的线显示路径。

如何在播放的第二秒开始从第一行的末尾开始绘制第二行。

    public MainWindow()
    {
        InitializeComponent();
        Line line = new Line();
        canvas1.Children.Add(line);
        line.Stroke = Brushes.Red;
        line.StrokeThickness = 2;
        line.X1 = 100;
        line.Y1 = 100;
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation(line.Y2, 500, new Duration(new TimeSpan(0, 0, 1)));
        DoubleAnimation da1 = new DoubleAnimation(line.X2, 150, new Duration(new TimeSpan(0, 0, 1)));
        Storyboard.SetTargetProperty(da, new PropertyPath("(Line.Y2)"));
        Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)"));
        sb.Children.Add(da);
        sb.Children.Add(da1);
        line.BeginStoryboard(sb);

1 个答案:

答案 0 :(得分:0)

这就是我提出的: 只需创建一个点列表并调用这些方法。它将从点到点绘制线条。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Windows.Media.Animation;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Point> points = null;
        List<Line> lines = null;
        List<Storyboard> boards = null;

        public MainWindow()
        {
            InitializeComponent();

            points = new List<Point>();
            lines = new List<Line>();
            boards = new List<Storyboard>();

            points.Add(new Point(0, 0));
            points.Add(new Point(200, 200));
            points.Add(new Point(200, 0));
            points.Add(new Point(0, 0));
            points.Add(new Point(0, 200));
            points.Add(new Point(200, 200));
            points.Add(new Point(300, 200));

            SetupLines();
            DrawLines();
        }

        public void SetupLines()
        {
            int speed = 1;
            lines.Add(new Line());
            boards.Add(new Storyboard());

            for (int i = 0; i < points.Count - 1; i++ )
            {
                lines.Add(new Line());
                boards.Add(new Storyboard());

                canvas1.Children.Add(lines[i]);
                lines[i].Stroke = Brushes.Red;
                lines[i].StrokeThickness = 7;

                lines[i].X1 = points[i].X;
                lines[i].Y1 = points[i].Y;
                lines[i].X2 = points[i].X;
                lines[i].Y2 = points[i].Y;

                DoubleAnimation da = new DoubleAnimation(points[i].X, points[i+1].X, new Duration(new TimeSpan(0, 0, speed)));
                DoubleAnimation da1 = new DoubleAnimation(points[i].Y, points[i+1].Y, new Duration(new TimeSpan(0, 0, speed)));

                Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
                Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.Y2)"));

                boards[i].Children.Add(da);
                boards[i].Children.Add(da1);

                da.BeginTime = new TimeSpan(0, 0, i * speed);
                da1.BeginTime = new TimeSpan(0, 0, i * speed);

                lines.Add(new Line());
                boards.Add(new Storyboard());
            } 
        }

        public void DrawLines()
        {
            for (int i = 0; i < boards.Count - 1; i++)
            {
                lines[i].BeginStoryboard(boards[i]);
            }
        }
    }
}