Home > OS >  How to animate dots in UserControl paint event?
How to animate dots in UserControl paint event?

Time:09-21

In paint event because i want to be able to control the dots size colors and more properties.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class LoadingLabel : UserControl
    {
        public LoadingLabel()
        {
            InitializeComponent();

        }

        private void LoadingLabel_Load(object sender, EventArgs e)
        {

        }

        private void LoadingLabel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
            Thread.Sleep(1);
            e.Graphics.FillEllipse(Brushes.Red, 1, 1, 0, 0);
            Thread.Sleep(1);
        }
    }
}

i tried first to make a simple dot that is disappearing after some time and then show again but it's not working i see a red still dot(point).

later when this will work i want to make 3 dots animating like a loading animation.

This is what i tried :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class LoadingLabel : UserControl
    {
        private bool animate = false;

        public LoadingLabel()
        {
            InitializeComponent();

            timer1.Enabled = true;
        }

        private void LoadingLabel_Load(object sender, EventArgs e)
        {

        }

        private void LoadingLabel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            if (animate == false)
            {
                e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
            }
            else
            {
                e.Graphics.FillEllipse(Brushes.Red, 5, 1, 20, 20);
            }
        }

        int count = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            count  ;

            if(count == 10 && animate == false)
            {
                animate = true;
            }

            if(count == 20 && animate)
            {
                animate = false;
                count = 0;
            }

            this.Invalidate();
        }
    }
}

the result is the first point draw then the second point draw but the first one is gone :

it looks like the point is moving to the right and back to the left.

Animated Dot

  • Related