FaceLoading.cs 2.7 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asa.FaceControl
{
    public partial class FaceLoading : ControlBase
    {
        private bool loop = false;
        private int circleIndex = -1;
        private readonly int circleCount = 12;
        private readonly int circleRadius = 10;
        private List<RectangleF> circleCenter;

        public FaceLoading()
        {
            InitializeComponent();
            circleCenter = new();
        }

        public void Start()
        {
            loop = true;
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
            thread.Start();
        }

        public void Stop()
        {
            loop = false;
            circleIndex = -1;
            Refresh();
        }

        protected override void CalcSize()
        {
            base.CalcSize();

            PointF center = new(Width / 2f, Height / 2f);
            float radius = Math.Min(center.X * 2f / 3f, center.Y * 2f / 3f);
            circleCenter = new();

            for (int i = 0; i < circleCount; i++)
            {
                double angle = 360d / circleCount * i;
                angle = Math.PI * angle / 180;

                float x = Convert.ToSingle(center.X + Math.Sin(angle) * radius);
                float y = Convert.ToSingle(center.Y - Math.Cos(angle) * radius);
                circleCenter.Add(new RectangleF(x - circleRadius, y - circleRadius, circleRadius * 2, circleRadius * 2));
            }
        }

        protected override void DrawEnabled(Graphics g)
        {
            base.DrawEnabled(g);

            for (int i = 0; i < circleCenter.Count; i++)
            {
                if (i == circleIndex)
                    g.FillEllipse(new SolidBrush(theme.DOWN), circleCenter[i]);
                else
                    g.FillEllipse(new SolidBrush(theme.FORE), circleCenter[i]);
            }
        }

        protected override void DrawDisabled(Graphics g)
        {
            base.DrawDisabled(g);
        }

        private void Run()
        {
            circleIndex = 0;
            try
            {
                while (loop)
                {
                    Invoke(new Action(() => { Refresh(); }));
                    System.Threading.Thread.Sleep(100);
                    circleIndex++;
                    if (circleIndex == circleCount) circleIndex = 0;
                }
            }
            catch (Exception ex)
            {

            }
        }

    }
}