在画布中定位节点 [英] locate node in a canvas
问题描述
好吧我在画布上添加了一些节点然后我想回去为这些节点添加一些动画。但我不知道如何访问这些笔记。这是我的代码:
private void WholeNote(Point circlePoints,string te,int oc)
{
Node note = new Node();
note.Height = 10;
note.Width = 18;
note.Note = te;
note.Oct = Convert.ToString(oc);
Canvas.SetTop(注意,circlePoints.Y);
Canvas.SetLeft(注意,circlePoints.X);
locations.Add(canvas.Children.Add(note));
DoubleAnimation ani = new DoubleAnimation
{
From = 0,
To = note.Width,// Convert.ToDouble(i),
持续时间=新TimeSpan(0,0,2)
};
//添加动画
ani.FillBehavior = FillBehavior.HoldEnd;
note.BeginAnimation(HeightProperty,ani);
note.Effect = new DropShadowEffect
{
Color = new Color {A = 1,R = 0,G = 139,B = 139},
Direction = 45,
ShadowDepth = 10,
Opacity = 0.8,
BlurRadius = 10
};
}
现在这个代码有效。
但我想 稍后 在笔记中添加更多动画。 ,
我该怎么做?
我正在跟踪笔记:
locations.Add(canvas.Children.Add(note));
嗨Btb4198,
根据你的描述,你想在画布中为控件添加一些动画,我在画布上做一个关于椭圆动画的例子,你可以看看:
私人故事板myStoryboard;
public Window14()
{
InitializeComponent();
Canvas canvas = new Canvas();
Ellipse ellipse = new Ellipse();
ellipse.Name =" ellipse" ;;
this.RegisterName(ellipse.Name,ellipse);
ellipse.Width = 100;
ellipse.Height = 50;
SolidColorBrush color = new SolidColorBrush();
color.Color = Colors.Red;
ellipse.Fill = color;
Canvas.SetLeft(ellipse,20);
Canvas.SetTop(ellipse,20);
//locations.Add(canvas.Children.Add(ellipse));
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 1.0;
myDoubleAnimation.To = 0.0;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
myDoubleAnimation.AutoReverse = true;
myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
Storyboard.SetTargetName(myDoubleAnimation,ellipse.Name);
Storyboard.SetTargetProperty(myDoubleAnimation,new PropertyPath(Rectangle.OpacityProperty));
ellipse.Loaded + = Ellipse_Loaded;
canvas.Children.Add(ellipse);
this.Content = canvas;
}
private void Ellipse_Loaded(object sender,RoutedEventArgs e)
{
myStoryboard.Begin(this);
}
关于动画的MSDN文章,你可以看看:
https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/animation -overview
ok I add some node to my canvas and then I want to go back and add some animation to those nodes. but I do not know how to access this notes. here is my code :
private void WholeNote(Point circlePoints, string te, int oc) { Node note = new Node(); note.Height = 10; note.Width = 18; note.Note = te; note.Oct = Convert.ToString(oc); Canvas.SetTop(note, circlePoints.Y); Canvas.SetLeft(note, circlePoints.X); locations.Add(canvas.Children.Add(note)); DoubleAnimation ani = new DoubleAnimation { From = 0, To = note.Width,// Convert.ToDouble(i), Duration = new TimeSpan(0, 0, 2) }; //Add animation ani.FillBehavior = FillBehavior.HoldEnd; note.BeginAnimation(HeightProperty, ani); note.Effect = new DropShadowEffect { Color = new Color { A = 1, R = 0, G = 139, B = 139 }, Direction = 45, ShadowDepth = 10, Opacity = 0.8, BlurRadius = 10 }; }
now this code works.
but I want to later on add some more animation to the notes. ,
how do I do I that ?
I am keeping track of the notes with :
locations.Add(canvas.Children.Add(note));
Hi Btb4198,
According to your description, you want to add some animation to controls in canvas, I do one sample about Ellipse animation in canvas, you can take a look:
private Storyboard myStoryboard; public Window14() { InitializeComponent(); Canvas canvas = new Canvas(); Ellipse ellipse = new Ellipse(); ellipse.Name = "ellipse"; this.RegisterName(ellipse.Name,ellipse); ellipse.Width = 100; ellipse.Height = 50; SolidColorBrush color = new SolidColorBrush(); color.Color = Colors.Red; ellipse.Fill = color; Canvas.SetLeft(ellipse, 20); Canvas.SetTop(ellipse, 20); //locations.Add(canvas.Children.Add(ellipse)); DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 1.0; myDoubleAnimation.To = 0.0; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); myDoubleAnimation.AutoReverse = true; myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever; myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); Storyboard.SetTargetName(myDoubleAnimation, ellipse.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty)); ellipse.Loaded += Ellipse_Loaded; canvas.Children.Add(ellipse); this.Content = canvas; } private void Ellipse_Loaded(object sender, RoutedEventArgs e) { myStoryboard.Begin(this); }MSDN article about animation, you can take a look:
https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/animation-overview
这篇关于在画布中定位节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!