[英]How to reduce flickering when using Graphics.DrawString in C#, for a timer?
下面是将文本绘制到进度条的C#代码。 它用于向进度条添加文本,显示剩余时间倒计时。 我使用Graphics类绘制字符串而不是Windows窗体标签,因为放置在进度条上时标签背景无法设置为透明。
但是,使用此代码,文本每次更新时都会闪烁(此方法在每秒钟滴答一次的计时器内调用),因此它会不断闪烁并且无法使用。
/// <summary>
/// Adds time remaining text into a System.Windows.Forms.ProgressBar
/// </summary>
/// <param name="target">The target progress bar to add text into</param>
/// <param name="remainingTimeText">The text to add into the progress bar.</param>
private void set_progress_bar_text( System.Windows.Forms.ProgressBar target, string remainingTimeText )
{
// Make sure we do not have a null progress bar.
if( target == null )
{
throw new ArgumentException( "Null Target" );
}
// Use the progress bar label font and size.
Font textFont = new Font( labelProgress.Font.Name, labelProgress.Font.Size );
// gr will be the graphics object we use to draw on the progress bar.
using( Graphics gr = target.CreateGraphics() )
{
gr.DrawString( remainingTimeText,
textFont,
new SolidBrush( Color.Black ), // The brush we will use to draw the string, using a black colour.
// The position on the progress bar to put the text.
new PointF(
// X location of text, to be placed at the centre of the progress bar.
progressBar.Width / 2 - ( gr.MeasureString( remainingTimeText,
textFont ).Width / 2.0F ),
// Y Location
progressBar.Height / 2 - ( gr.MeasureString( remainingTimeText,
textFont ).Height / 2.0F ) ) );
}
}
我已尝试在此方法中设置DoubleBuffered = true
,如Stack Overflow上的相关问题所示,但它不会阻止闪烁。 我不能减少文本更新的次数,因为文本是必须每秒更新一次的倒计时时钟。 有没有办法防止双缓冲闪烁,还是有其他潜在的解决方案?
ProgressBar似乎是试图隐藏绘画的控件之一。 所以我们需要自己动手。 只需将ProgressBar与此进行交换即可。
添加了CreateParams覆盖以减少评论中提到的闪烁。
public class MyLovelyProgressBar : ProgressBar
{
public MyLovelyProgressBar()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
private string foregroundText;
public string ForegroundText
{
get { return foregroundText; }
set
{
if (foregroundText != value)
{
Invalidate();
foregroundText = value;
}
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams result = base.CreateParams;
result.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return result;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case 15: //WmPaint
using (var graphics = Graphics.FromHwnd(Handle))
PaintForeGroundText(graphics);
break;
}
}
private void PaintForeGroundText(Graphics graphics)
{
if (!string.IsNullOrEmpty(ForegroundText))
{
var size = graphics.MeasureString(ForegroundText, this.Font);
var point = new PointF(Width / 2.0F - size.Width / 2.0F, Height / 2.0F - size.Height / 2.0F);
graphics.DrawString(ForegroundText, this.Font, new SolidBrush(Color.Black), point);
}
}
}
然后只需在Timer事件中更改ProgressBar的ForegroundText。
myLovelyProgressBar1.ForegroundText = "A constantly changing lovely text";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.