看到野比叔发了一个山寨进度条 我也来发一个
这是使用form为载体的 优点:ShowDialog后无法操作调用方
如果只是作为1个简单的进度条 可以把 Form 改成 重载成 Control
因为是自己用的 所以没有把字符串什么的自定义出来 可以自己改进
C#:
调用:
using (MyProgressBar ms = new MyProgressBar()) { ms.ShowDialog(); }
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[] { backcol, Color.FromArgb(60, backcol), Color.FromArgb(30, backcol), Color.FromArgb(60, backcol), Color.FromArgb(100, backcol), Color.FromArgb(130, backcol) }; sb.Positions = new float[] { 0f, 3.5f / 5f, 4.0f / 5f, 4.5f / 5f, 4.7f / 5f, 1f }; br.InterpolationColors = sb; e.Graphics.FillRectangle(br, DisplayRectangle); } //动态背景 using (LinearGradientBrush br = new LinearGradientBrush(backlint, Color.Transparent, Color.Transparent, 0f)) { ColorBlend sb = new ColorBlend(); sb.Colors = new Color[] { Color.Transparent, Color.FromArgb(60, jianbiancol), Color.FromArgb(150, jianbiancol), Color.FromArgb(180, jianbiancol), Color.FromArgb(100, jianbiancol), Color.FromArgb(10, jianbiancol) }; sb.Positions = new float[] { 0f, 1.5f / 5f, 2.8f / 5f, 3.7f / 5f, 4.5f / 5f, 1f }; br.InterpolationColors = sb; e.Graphics.FillRectangle(br, backlint); } // using (SolidBrush br = new SolidBrush(titlecol)) { using (StringFormat sf = new StringFormat()) { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString("正在初始化,请稍后...", SystemFonts.DefaultFont, br, this.DisplayRectangle, sf); } } //边框 using (Pen p = new Pen(bordercol, 2)) { e.Graphics.DrawRectangle(p, DisplayRectangle); } using (Pen p = new Pen(bordercol, 1)) { e.Graphics.DrawLine(p, 0, 0, 1, 1); e.Graphics.DrawLine(p, 1, Height - 2, 0, Height - 1); e.Graphics.DrawLine(p, Width - 2, 1, Width - 1, 0); e.Graphics.DrawLine(p, Width - 2, Height - 2, Width - 1, Height - 1); } } } #region "移动窗体" private Point mousepoint; private bool ismousedown; private void User_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e) { mousepoint = PointToClient(MousePosition); ismousedown = true; } private void User_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e) { ismousedown = false; } private void User_MouseMove(System.Object sender, System.Windows.Forms.MouseEventArgs e) { if (ismousedown) { int x = MousePosition.X - mousepoint.X; int y = MousePosition.Y - mousepoint.Y; int max = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - base.Width; int may = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - base.Height; if (x < 0) { x = 0; } else if (x > max) { x = max; } if (y < 0) { y = 0; } else if (y > may) { y = may; } Location = new Point(x, y); } } #endregion #region "定时回调" private delegate void Async(); private System.Threading.Timer timer; private void TimerChangeInvoke(object state) { this.BeginInvoke(new Async(TimerChange)); } private void TimerChange() { Rectangle oldlint = backlint; backlint.X += 5; if (backlint.X > Width) { backlint.X = -backlint.Width; } this.Invalidate(oldlint); this.Invalidate(backlint); this.Update(); } #endregion }