wpf 绘制一个椭圆,然后将其移动到另一个位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16121748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing an ellipse and then move it to another position
提问by user1866990
I have drawn an ellipse using an EllipsePointsarray which defines the height width and color of the ellipse.
我使用一个EllipsePoints数组绘制了一个椭圆,该数组定义了椭圆的高度宽度和颜色。
Then using a for loop to get the position of the ellipse using the ellipse points and a random number to set its position:
然后使用 for 循环使用椭圆点和随机数来获取椭圆的位置以设置其位置:
Random rand = new Random();
Int32 randomNumber = rand.Next(0, 310);
Int32 randomNumber2 = rand.Next(0, 500);
for (int j = 0; j < 60; j++)
{
ellipsePoints[j] = new Ellipse() { Width = 20, Height = 20, Fill = Brushes.Red };
canvas1.Children.Add(ellipsePoints[j]);
}
for (int i = 0; i < 60; i++)
{
Canvas.SetLeft(ellipsePoints[i], randomNumber2);
Canvas.SetTop(ellipsePoints[i], randomNumber);
}
What could I do to make the ellipse vanish after a certain amount of time and then appear in another random location?
我该怎么做才能使椭圆在一段时间后消失,然后出现在另一个随机位置?
回答by Patrick D'Souza
There are 2 important aspects to this question.
这个问题有两个重要方面。
- Timer- In WPF we use the System.Windows.Threading.DispatcherTimer.
Remove elements- One way is to maintain a copy of the UI element before adding it to the Canvas. I have saved it in a class variable so that I can later remove it from the canvas using the following method
PaintCanvas.Children.Remove(ellipse);
- 计时器- 在 WPF 中,我们使用System.Windows.Threading.DispatcherTimer。
删除元素- 一种方法是在将 UI 元素添加到画布之前保留它的副本。我已将其保存在一个类变量中,以便稍后可以使用以下方法将其从画布中删除
PaintCanvas.Children.Remove(椭圆);
Create you WPF and add a canvas called PaintCanvas
创建 WPF 并添加一个名为 PaintCanvas 的画布
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="889" Width="1080">
<Canvas Name="PaintCanvas">
<Button Canvas.Left="46" Canvas.Top="274" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
</Canvas >
</Window>
The Code. I have documented it.
编码。我已经记录了它。
public partial class MainWindow : Window
{
int loopCounter;
private System.Windows.Threading.DispatcherTimer timer;
Random rand = new Random();
Ellipse ellipse = null;
public MainWindow()
{
InitializeComponent();
//Initialize the timer class
timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1); //Set the interval period here.
timer.Tick += timer1_Tick;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
loopCounter = 10;
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//Remove the previous ellipse from the paint canvas.
PaintCanvas.Children.Remove(ellipse);
if (--loopCounter == 0)
timer.Stop();
//Add the ellipse to the canvas
ellipse=CreateAnEllipse(20,20 );
PaintCanvas.Children.Add(ellipse);
Canvas.SetLeft(ellipse, rand.Next(0, 310));
Canvas.SetTop(ellipse, rand.Next(0, 500));
}
// Customize your ellipse in this method
public Ellipse CreateAnEllipse(int height,int width)
{
SolidColorBrush fillBrush = new SolidColorBrush() { Color = Colors.Red };
SolidColorBrush borderBrush = new SolidColorBrush() { Color = Colors.Black };
return new Ellipse()
{
Height = height,
Width = width,
StrokeThickness = 1,
Stroke = borderBrush,
Fill = fillBrush
};
}
}

