WPF locate discreted points via periodically and set transparency via the alpha,the first parameter of Argb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//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));
            }
        }
    }
}

  

 

 

 

 

posted @   FredGrit  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示