Загрузка данных


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WindowsFormsApp34
{
    public delegate void LoadChangedEventHandler(
        object sender,
        LoadChangedEventArgs e
    );

    public partial class Thermometer : UserControl
    {
        private int currentValue = 0;
        private int maximum = 100;

        public event EventHandler ThermometerClick;

        public event LoadChangedEventHandler LoadChanged;

        public event LoadChangedEventHandler HighLoad;

        public Thermometer()
        {
            InitializeComponent();

            this.Size = new Size(200, 350);

            this.DoubleBuffered = true;

            this.BackColor = Color.FromArgb(30, 30, 45);
        }

        public int Maximum
        {
            get { return maximum; }

            set
            {
                if (value > 0)
                {
                    maximum = value;

                    if (currentValue > maximum)
                        currentValue = maximum;

                    Invalidate();
                }
            }
        }

        public int Value
        {
            get { return currentValue; }

            set
            {
                int newValue = value;

                if (newValue < 0)
                    newValue = 0;

                if (newValue > maximum)
                    newValue = maximum;

                if (currentValue == newValue)
                    return;

                int oldValue = currentValue;

                currentValue = newValue;

                Invalidate();

                if (LoadChanged != null)
                {
                    LoadChanged(
                        this,
                        new LoadChangedEventArgs(currentValue)
                    );
                }

                if (oldValue < 80 && currentValue >= 80)
                {
                    if (HighLoad != null)
                    {
                        HighLoad(
                            this,
                            new LoadChangedEventArgs(currentValue)
                        );
                    }
                }
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (ThermometerClick != null)
            {
                ThermometerClick(this, EventArgs.Empty);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.Clear(this.BackColor);

            int centerX = Width / 2;

            int top = 45;
            int bottom = Height - 75;

            int tubeHeight = bottom - top;

            int tubeWidth = 44;

            int tubeX = centerX - tubeWidth / 2;

            int fillHeight =
                (int)(tubeHeight *
                (currentValue / (double)maximum));

            Color liquidColor;

            double percent =
                currentValue * 100.0 / maximum;

            if (percent < 40)
                liquidColor = Color.LimeGreen;
            else if (percent < 80)
                liquidColor = Color.Orange;
            else
                liquidColor = Color.Red;

            using (Pen outline = new Pen(Color.White, 3))
            {
                g.DrawRectangle(
                    outline,
                    tubeX,
                    top,
                    tubeWidth,
                    tubeHeight
                );
            }

            using (SolidBrush liquid =
                new SolidBrush(liquidColor))
            {
                g.FillRectangle(
                    liquid,
                    tubeX + 3,
                    bottom - fillHeight,
                    tubeWidth - 5,
                    fillHeight
                );

                g.FillEllipse(
                    liquid,
                    centerX - 32,
                    bottom - 5,
                    64,
                    64
                );
            }

            using (Pen outline = new Pen(Color.White, 2))
            {
                g.DrawEllipse(
                    outline,
                    centerX - 32,
                    bottom - 5,
                    64,
                    64
                );
            }

            using (Font font = new Font("Arial", 10))
            using (SolidBrush brush =
                new SolidBrush(Color.White))
            {
                for (int i = 0; i <= 100; i += 10)
                {
                    int y = bottom -
                        (int)(tubeHeight * i / 100.0);

                    g.DrawLine(
                        Pens.White,
                        tubeX + tubeWidth + 5,
                        y,
                        tubeX + tubeWidth + 15,
                        y
                    );

                    g.DrawString(
                        i.ToString(),
                        font,
                        brush,
                        tubeX + tubeWidth + 18,
                        y - 8
                    );
                }
            }

            using (Font titleFont =
                new Font("Arial", 12, FontStyle.Bold))
            using (SolidBrush brush =
                new SolidBrush(Color.White))
            {
                string title = "CPU LOAD";

                SizeF size = g.MeasureString(
                    title,
                    titleFont
                );

                g.DrawString(
                    title,
                    titleFont,
                    brush,
                    (Width - size.Width) / 2,
                    10
                );
            }

            using (Font valueFont =
                new Font("Arial", 18, FontStyle.Bold))
            using (SolidBrush brush =
                new SolidBrush(liquidColor))
            {
                string text = currentValue + "%";

                SizeF size = g.MeasureString(
                    text,
                    valueFont
                );

                g.DrawString(
                    text,
                    valueFont,
                    brush,
                    (Width - size.Width) / 2,
                    Height - 40
                );
            }
        }
    }
}