仿 Chrome 进度条

/*******************************************************************************
 * ProgressCircle.cs
 * A Google Chrome style progress indicator.
 * -----------------------------------------------------------------------------
 * Project: Conmajia.Controls
 * Author: Conmajia
 * Url: conmajia@gmail.com
 * Initiate: 25th July, 2012
 * Version: 1.0.0.0
 * History:
 *      25th July, 2012
 *      Initiated.
 * ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ProgressCircleControl
{
    public class ProgressCircle : Control
    {
        #region variables
        Timer timer;

        Graphics canvasGraphic;
        Image canvas;
        Image icon;

        Rectangle rect;

        int blinkCount = 2;
        int blinkSpeed = 10;

        Pen spokePen = Pens.White;
        int spokeCount = 4;
        Brush pieBrush = Brushes.LawnGreen;

        int value = 0;

        PointF center;
        #endregion

        #region properties
        [DefaultValue(2), Category("Appearance"), Description("Blink count. 0 for disable.")]
        public int BlinkCount
        {
            get { return blinkCount; }
            set
            {
                if (value > -1)
                {
                    blinkCount = value; Refresh();
                }
            }
        }

        [DefaultValue(10), Category("Appearance"), Description("Blink speed.")]
        public int BlinkSpeed
        {
            get { return blinkSpeed; }
            set
            {
                if (value > 0 && value < 256)
                    blinkSpeed = value;
            }
        }

        [DefaultValue(4), Category("Appearance"), Description("Spoke count. Maximum value is 10. 0 for disable.")]
        public int SpokeCount
        {
            get { return spokeCount; }
            set
            {
                spokeCount = value;
                Refresh();
            }
        }

        [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Color of spokes.")]
        public Color SpokeColor
        {
            get { return spokePen.Color; }
            set
            {
                spokePen.Color = value;
                Refresh();
            }
        }

        [Category("Appearance"), Description("Icon to display. Maximum size is half of the control.")]
        public Image Image
        {
            get { return icon; }
            set
            {
                //if (value != null && (value.Width > this.Width / 2 && value.Height > this.Height / 2))
                //    return;

                icon = value;
                Refresh();
            }
        }

        [DefaultValue(0), Category("Behavior"), Description("Value of percentage.")]
        public int Value
        {
            get { return value; }
            set
            {
                if (value < 0) value = 0;
                if (value > 100) value = 100;
                this.value = value;
                Refresh();

                if (value == 100 && BlinkCount != 0)
                    timer.Start();
                else
                    if (timer.Enabled)
                        timer.Stop();
            }
        }
        #endregion

        #region initiators
        public ProgressCircle()
        {
            this.DoubleBuffered = true;
            this.Width = 32;

            ForeColor = Color.FromArgb(146, 223, 99);

            spokePen = new Pen(BackColor, 1);
            pieBrush = new SolidBrush(ForeColor);

            BackColor = Color.White;
            ForeColor = Color.LawnGreen;

            BlinkCount = 2;
            SpokeColor = Color.White;
            SpokeCount = 4;
            center = new PointF();

            timer = new Timer();
            timer.Interval = 10;
            timer.Tick += new EventHandler(timer_Tick);

            OnResize(null);
        }

        #endregion

        #region callbacks
        int blink = 0;
        int count = 255;
        void timer_Tick(object sender, EventArgs e)
        {
            if (!timer.Enabled)
                return;

            if (blink == BlinkCount)
            {
                blink = 0;
                count = 255;
                timer.Stop();
                Refresh();
            }
            else
            {
                count -= 5;
                if (count == 0)
                {
                    count = 255;
                    blink++;
                }
                else
                {
                    Refresh();
                }
            }
        }
        #endregion

        #region painters
        void drawIcon(Graphics g, Image img)
        {
            if (img == null)
                return;

            g.DrawImageUnscaled(
                img,
                (this.ClientSize.Width - img.Width) / 2,
                (this.ClientSize.Height - img.Height) / 2
                );
        }

        void drawSpokes(Graphics g, int count)
        {
            if (count < 1)
                return;

            if (count > 10)
                count = 10;

            float angleStep = 360f / (count * 2);
            float angle = 0f;
            for (int i = 0; i < count; i++)
            {
                PointF p1 = angleToPoint(angle, rect.Width / 2);
                PointF p2 = angleToPoint(angle + 180f, rect.Width / 2);

                g.DrawLine(spokePen, p1, p2);

                // draw shadow
                // not ready

                angle += angleStep;
            }
        }

        void drawProgress(Graphics g, float percent)
        {
            if (percent < 0 || percent > 100)
                return;

            g.FillPie(
                pieBrush,
                rect,
                -90,
                (float)(360f * Value / 100f)
                );
        }

        void drawBackground(Graphics g)
        {
            g.Clear(Color.Transparent);
            ((SolidBrush)pieBrush).Color = BackColor;
            g.FillEllipse(pieBrush, rect);
            ((SolidBrush)pieBrush).Color = this.ForeColor;
        }
        #endregion

        #region helpers
        PointF angleToPoint(float angle, float radius)
        {
            return new PointF(
                center.X + radius * (float)Math.Cos(angle * Math.PI / 180),
                center.Y + radius * (float)Math.Sin(angle * Math.PI / 180)
                );
        }
        #endregion

        #region events
        protected override void OnBackColorChanged(EventArgs e)
        {
            base.OnBackColorChanged(e);
        }

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            ((SolidBrush)pieBrush).Color = ForeColor;
        }

        protected override void OnResize(EventArgs e)
        {
            this.Height = this.Width;
            rect = this.ClientRectangle;
            rect.Width -= 1;
            rect.Height -= 1;
            //rect.X = (this.ClientSize.Width - rect.Width) / 2;
            //rect.Y = (this.ClientSize.Height - rect.Height) / 2;

            center = new PointF(
                rect.Width / 2,
                rect.Height / 2
                );

            if (!this.ClientSize.IsEmpty)
            {
                if (canvas != null)
                    canvas.Dispose();
                canvas = new Bitmap(rect.Width, rect.Height);

                if (canvasGraphic != null)
                    canvasGraphic.Dispose();
                canvasGraphic = Graphics.FromImage(canvas);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.Clear(this.Parent.BackColor);

            canvasGraphic.SmoothingMode = SmoothingMode.HighQuality;

            drawBackground(canvasGraphic);

            if (!timer.Enabled || blink == BlinkCount)
                drawProgress(canvasGraphic, value);
            else
            {
                ((SolidBrush)pieBrush).Color = Color.FromArgb(count, ForeColor.R, ForeColor.G, ForeColor.B);
                canvasGraphic.FillEllipse(pieBrush, rect);
                ((SolidBrush)pieBrush).Color = ForeColor;
            }

            drawSpokes(canvasGraphic, SpokeCount);

            if (icon != null)
                drawIcon(canvasGraphic, icon);

            e.Graphics.DrawImageUnscaled(
                canvas,
                (this.ClientSize.Width - canvas.Width) / 2,
                (this.ClientSize.Height - canvas.Height) / 2
                );
        }
        #endregion
    }
}

Chrome仿站插件是一种可以在Chrome浏览器中使用的插件,它可以帮助用户进行网站仿制和设计开发。插件通过提供一系列的功能和工具,使用户能够在浏览器中创建、修改和测试网站的外观和功能。 首先,Chrome仿站插件通常提供一个网站源代码编辑器,用户可以使用该工具查看和编辑网站的HTML、CSS和JavaScript代码。这让用户能够对网站的布局和样式进行调整,改变元素的位置、大小和颜色,以满足自己的需求。 其次,插件还提供了实时预览功能,用户可以在浏览器窗口中即时查看网站的最新外观。这样,用户能够看到对代码所做的更改如何影响网站的视觉效果,并及时进行调整和优化。 此外,Chrome仿站插件还提供了模板和组件库,用户可以选择适合自己的网站风格和需求的模板和组件。这些模板和组件通常包含了常见的网站元素,如导航栏、图片滑动器、表单等,用户可以直接使用它们,而无需从头编写代码。 最后,一些Chrome仿站插件还支持网站性能测试和移动端适配等功能。用户可以通过插件来测试网站在不同设备和网络环境下的响应速度和加载性能,以便进行优化和改进。 总的来说,Chrome仿站插件能够帮助用户快速、方便地进行网站设计和开发,提供了丰富的功能和工具,让用户能够实时预览和调整网站的外观和功能,以达到更好的用户体验和功能实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值