首页 >后端开发 >C++ >如何使用 C# 在 WPF 画布上绘制线条动画?

如何使用 C# 在 WPF 画布上绘制线条动画?

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-05 01:53:43625浏览

How to Animate a Line Drawing Across a WPF Canvas Using C#?

如何用 C 语言在画布上制作一条线的动画

问题:如何在屏幕上缓慢绘制一条线?我想在 WPF 项目中使用 C# 代码。

答案:

要在 C# 中为画布上的线条设置动画,请按照以下步骤操作:

  1. 在 WPF 表单上创建一个画布控件。
  2. 创建一个 LineViewModel 类代表您想要设置动画的线条。
  3. 将 LineViewModel 绑定到画布控件。
  4. 在 LineViewModel 类中,定义一个用于为线条设置动画的计时器。
  5. 在Timer_Tick事件处理程序中,更新线的坐标以创建动画效果。
  6. 设置Animate属性设置为 true 以启动动画。

以下示例代码片段演示了如何使用提供的步骤在画布上创建线条并为其设置动画:

XAML :

<Canvas x:Name="MyCanvas" Height="500" Width="500">
  <Line x:Name="MyLine" X1="0" Y1="0" X2="100" Y2="100" Stroke="Black" StrokeThickness="2"/>
</Canvas>

C#代码:

public partial class MainWindow : Window
{
    private LineViewModel _lineViewModel;

    public MainWindow()
    {
        InitializeComponent();

        // Create the LineViewModel.
        _lineViewModel = new LineViewModel();

        // Bind the LineViewModel to the Line control.
        MyLine.SetBinding(Line.X1Property, new Binding("X1") { Source = _lineViewModel });
        MyLine.SetBinding(Line.Y1Property, new Binding("Y1") { Source = _lineViewModel });
        MyLine.SetBinding(Line.X2Property, new Binding("X2") { Source = _lineViewModel });
        MyLine.SetBinding(Line.Y2Property, new Binding("Y2") { Source = _lineViewModel });

        // Start the animation.
        _lineViewModel.Animate = true;
    }
}

public class LineViewModel : INotifyPropertyChanged
{
    #region Timer-based Animation

    private System.Threading.Timer Timer;
    private static Random Rnd = new Random();

    private bool _animate;
    public bool Animate
    {
        get { return _animate; }
        set
        {
            _animate = value;
            NotifyPropertyChanged("Animate");
            if (value)
                StartTimer();
            else
                StopTimer();
        }
    }

    private int _animationSpeed = 1;
    public int AnimationSpeed
    {
        get { return _animationSpeed; }
        set
        {
            _animationSpeed = value;
            NotifyPropertyChanged("AnimationSpeed");
            if (Timer != null)
                Timer.Change(0, 100 / value);
        }
    }

    private static readonly List<int> _animationSpeeds = new List<int> { 1, 2, 3, 4, 5 };
    public List<int> AnimationSpeeds
    {
        get { return _animationSpeeds; }
    }

    public void StartTimer()
    {
        StopTimer();
        Timer = new Timer(x => Timer_Tick(), null, 0, 100 / AnimationSpeed);
    }

    public void StopTimer()
    {
        if (Timer != null)
        {
            Timer.Dispose();
            Timer = null;
        }
    }

    private void Timer_Tick()
    {
        X1 = X1 + Rnd.Next(-2, 3);
        Y1 = Y1 + Rnd.Next(-2, 3);
        X2 = X2 + Rnd.Next(-2, 3);
        Y2 = Y2 + Rnd.Next(-2, 3);
    }

    #endregion

    #region Coordinates

    private double _x1;
    public double X1
    {
        get { return _x1; }
        set
        {
            _x1 = value;
            NotifyPropertyChanged("X1");
        }
    }

    private double _y1;
    public double Y1
    {
        get { return _y1; }
        set
        {
            _y1 = value;
            NotifyPropertyChanged("Y1");
        }
    }

    private double _x2;
    public double X2
    {
        get { return _x2; }
        set
        {
            _x2 = value;
            NotifyPropertyChanged("X2");
        }
    }

    private double _y2;
    public double Y2
    {
        get { return _y2; }
        set
        {
            _y2 = value;
            NotifyPropertyChanged("Y2");
        }
    }

    #endregion

    #region Other Properties

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    private double _thickness;
    public double Thickness
    {
        get { return _thickness; }
        set
        {
            _thickness = value;
            NotifyPropertyChanged("Thickness");
        }
    }

    public Color Color1 { get; set; }
    public Color Color2 { get; set; }

    private double _opacity = 1;
    public double Opacity
    {
        get { return _opacity; }
        set
        {
            _opacity = value;
            NotifyPropertyChanged("Opacity");
        }
    }

    #endregion

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }));
    }

    #endregion
}

使用这种方法,您可以创建一条以可自定义的速度在画布上缓慢重绘的线条。

以上是如何使用 C# 在 WPF 画布上绘制线条动画?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn