Files
ImageCompressor/MainForm/NewProgressPanel.cs
2026-02-02 21:56:45 +08:00

72 lines
2.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// ProgressBar类有点问题重写一个简单的ProgressBar。
/// </summary>
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);
}
/// <summary>
/// 克隆原来的Panel以实现设计的参数传递。
/// </summary>
/// <param name="raw"></param>
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;
/// <summary>
/// 绘制背景,快但是不能耗时太久,会闪
/// </summary>
/// <param name="e"></param>
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);
}
/// <summary>
/// 绘制耗时较多的部分
/// </summary>
/// <param name="e"></param>
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);
}
}
}