c# - 绘制一个椭圆,然后将其移动到另一个位置

标签 c# wpf ellipse

我使用 EllipsePoints 数组绘制了一个椭圆,该数组定义了椭圆的高度、宽度和颜色。

然后使用 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);
}

我该怎么做才能使椭圆在一段时间后消失,然后出现在另一个随机位置?

最佳答案

这个问题有两个重要方面。

  • 定时器 - 在 WPF 中,我们使用 System.Windows.Threading.DispatcherTimer
  • 删除元素 - 一种方法是在将 UI 元素添加到 Canvas 之前保留其副本。我已将其保存在类变量中,以便稍后可以使用以下方法将其从 Canvas 中删除

    PaintCanvas.Children.Remove(椭圆);

创建 WPF 并添加一个名为 PaintCanvas 的 Canvas

<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>

代码。我已经记录下来了。

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
        };     
    }
}

关于c# - 绘制一个椭圆,然后将其移动到另一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121748/

相关文章:

c# - 列表框可以显示字典中的键和值

c++ - 求旋转椭圆的角度

java - 为什么我的 intlist 值没有增加,或者为什么 ellipse() 函数没有响应它?

javascript - d3 GeoJSON geoCircle 椭圆等效

c# - DirectoryInfo.Name的异常行为

c# - 无法设置上下文菜单条的位置?

c# - 如何在实时图表中逐个添加点到系列

c# - 自定义 Linq 查询

c# - 如何定义我想在 SandcaSTLe 的 HFB 文档中看到哪种语言代码?

wpf - 我可以实现自己的 View 解析服务并让 RequestNavigate 使用它吗?