using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Drawing2D; using System.Diagnostics; using System.IO; namespace ImageCompressor { /// /// ProgressBar类有点问题,重写一个简单的ProgressBar。 /// internal class NewProgressPanel:Panel { public Font TextFont; public Brush TextForeColor; public NewProgressPanel(Font textFont,Color textForeColor) { this.TextFont = textFont; this.TextForeColor = new SolidBrush(textForeColor); SetStyle(ControlStyles.ResizeRedraw, true); this.foreBrush = new SolidBrush(Color.Gray); } /// /// 克隆原来的Panel以实现设计的参数传递。 /// /// public void Clone(Panel raw) { this.Size = raw.Size; this.Bounds = raw.Bounds; this.Anchor = raw.Anchor; this.BorderStyle = raw.BorderStyle; this.Name = raw.Name; this.TabIndex = raw.TabIndex; this.BackColor= raw.BackColor; this.ForeColor = raw.ForeColor; } public double ProgressValue { get; set; } Brush foreBrush; /// /// 绘制背景,快但是不能耗时太久,会闪 /// /// protected override void OnPaintBackground(PaintEventArgs e) { e.Graphics.Clear(this.BackColor); e.Graphics.FillRectangle(foreBrush, 0, 0, (int)((double)e.ClipRectangle.Width * ProgressValue), e.ClipRectangle.Height); } /// /// 绘制耗时较多的部分 /// /// protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e);//控件必须通过重写它从 Control 继承的 OnPaint 方法来提供呈现逻辑。 //测量string应该较为耗时,放在此处绘制 SizeF size = e.Graphics.MeasureString(Text, TextFont); if (!string.IsNullOrEmpty(Text)) e.Graphics.DrawString(Text, TextFont, TextForeColor, Width / 2 - size.Width / 2, Height - size.Height - 2); } } }