日期:2014-05-18  浏览次数:20739 次

看到野比叔发了一个山寨进度条 我也来发一个
这是使用form为载体的 优点:ShowDialog后无法操作调用方

如果只是作为1个简单的进度条 可以把 Form 改成 重载成 Control

因为是自己用的 所以没有把字符串什么的自定义出来 可以自己改进



C#:
调用:
C# code

 using (MyProgressBar ms = new MyProgressBar())
            {
                ms.ShowDialog();
            }


C# code

public class MyProgressBar : Form
    {
        public MyProgressBar()
            : base()
        {
            MouseMove += User_MouseMove;
            MouseUp += User_MouseUp;
            MouseDown += User_MouseDown;
            Load += StartupLoad_Load;
            this.SuspendLayout();
            this.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.DoubleBuffered = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.ShowInTaskbar = false;
            this.Text = "进度条";
            this.Opacity = 0.7;
            this.TopMost = true;
            this.ResumeLayout(false);
        }
        private void StartupLoad_Load(object sender, System.EventArgs e)
        {
            this.Size = new System.Drawing.Size(258, 30);
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerChangeInvoke), null, 10, 30);
        }
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (timer != null)
                {
                    timer.Dispose();
                    timer = null;
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
        /// <summary>
        /// 阴影坐标
        /// </summary>
        /// <remarks></remarks>
        private Rectangle backlint = new Rectangle(0, 0, 101, 0);
        /// <summary>
        /// 重绘工作区域
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnSizeChanged(System.EventArgs e)
        {
            base.OnSizeChanged(e);
            using (GraphicsPath r = new GraphicsPath())
            {
                Rectangle[] re = new Rectangle[] {
                new Rectangle(2, 0, Width - 4, 1),
                new Rectangle(1, 1, Width - 2, 1),
                new Rectangle(0, 2, Width, Height - 4),
                new Rectangle(1, Height - 2, Width - 2, 1),
                new Rectangle(2, Height - 1, Width - 4, 1)
            };
                r.AddRectangles(re);
                this.Region = new Region(r);
                backlint.Height = this.Height;
            }
            this.Refresh();
        }
        /// <summary>
        /// 重绘窗体
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            if (e != null)
            {
                Color bordercol = Color.FromArgb(81, 169, 215);
                Color backcol = Color.FromArgb(228, 242, 249);
                Color titlecol = Color.FromArgb(24, 122, 174);
                Color jianbiancol = Color.FromArgb(182, 225, 247);
                e.Graphics.Clear(Color.White);
                using (LinearGradientBrush br = new LinearGradientBrush(DisplayRectangle, Color.Transparent, Color.Transparent, 80f))
                {
                    ColorBlend sb = new ColorBlend();
                    sb.Colors = new Color[] {