//xaml
<Window x:Class="WpfApp229.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp229"
mc:Ignorable="d" WindowState="Maximized"
Title="MainWindow" Height="450" Width="800">
<Canvas x:Name="cvs" />
</Window>
//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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;
namespace WpfApp229
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<Point> elpPoints { get; set; }
int idx = 0;
int len = 0;
Ellipse localtedElp { get; set; }
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
DrawEllipses();
len = elpPoints.Count;
localtedElp = new Ellipse();
localtedElp.Width = 50;
localtedElp.Height = 50;
localtedElp.Fill = new SolidColorBrush(Color.FromArgb(128,255,0,0));
if(!cvs.Children.Contains(localtedElp))
{
cvs.Children.Add(localtedElp);
}
PeriodicallyLocate();
}
private void PeriodicallyLocate()
{
Thread.Sleep(1000);
System.Timers.Timer tmr = new System.Timers.Timer();
tmr.Elapsed += Tmr_Elapsed;
tmr.Interval = 1000;
tmr.Start();
}
private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (idx++ < len-1)
{
Dispatcher.BeginInvoke(new Action(() =>
{
var x = elpPoints[idx].X;
var y = elpPoints[idx].Y;
Canvas.SetLeft(localtedElp, x-10);
Canvas.SetTop(localtedElp, y-10);
}));
}
}
private void DrawEllipses()
{
int width = (int)this.ActualWidth - 15;
int height = (int)this.ActualHeight - 15;
elpPoints = new List<Point>();
Random rnd = new Random();
for (int i = 0; i < 500; i++)
{
Ellipse elp = new Ellipse();
elp.Width = 30;
elp.Height = 30;
elp.Fill = new SolidColorBrush(Colors.Blue);
double x = rnd.Next(0, width);
double y = rnd.Next(0, height);
Canvas.SetLeft(elp, x);
Canvas.SetTop(elp, y);
cvs.Children.Add(elp);
elpPoints.Add(new Point(x, y));
}
}
}
}
![]()
![]()
![]()