以下的代码解决的是当进度到一半左右,文本与图片的叠加问题。
Code
public void DrawProgressBar(Graphics g, Rectangle bounds, ProgressBar bar)
{
Bitmap back = null, bval = null;
if (rsMgr.Enabeld)
{
back = rsMgr.GetBitmap("PROGRESSBAR"); //背景图片
bval = rsMgr.GetBitmap("PROGRESSBAR_VALUE"); //前景图片
}
//获得比例
decimal v = bar.Value / (decimal)(bar.Maximum - bar.Minimum);
Rectangle rect = bounds;
rect.Inflate(-1, -1);
if (back != null)
{
//绘制背景图片
TextureBrush b = new TextureBrush(back);
b.TranslateTransform(0, rect.Top);
b.ScaleTransform(1, rect.Height / (float)back.Height);
g.FillRectangle(b, rect);
b.Dispose();
ControlPaint.DrawBorder(g, rect, rsMgr.GetColor("BORDERCOLOR"), ButtonBorderStyle.Solid);
}
else
{
//用颜色填充
g.FillRectangle(new SolidBrush(bar.BackColor), rect);
ControlPaint.DrawBorder(g, rect, Color.LightGray, ButtonBorderStyle.Solid);
}
rect.Inflate(-2, -2);
rect.Width = (int)(rect.Width * v);
if (bval != null)
{
//绘制前景图片
TextureBrush b = new TextureBrush(bval);
b.TranslateTransform(0, rect.Top);
b.ScaleTransform(1, rect.Height / (float)bval.Height);
g.FillRectangle(b, rect);
b.Dispose();
}
else
{
//颜色填充
g.FillRectangle(SystemBrushes.Highlight, rect);
}
//输出文本
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Region oldClip = g.Clip;
if (bval != null)
{
//先输出整个文本
Bitmap inb = (Bitmap)back.Clone();
//这里将图片反转
BitmapHelper.Invert(inb);
TextureBrush b = new TextureBrush(inb);
g.DrawString(bar.Text, bar.Font, b, bounds, sf);
//剪辑后在前景范围内输出
g.SetClip(rect);
inb = (Bitmap)bval.Clone();
BitmapHelper.Invert(inb);
b = new TextureBrush(inb);
g.DrawString(bar.Text, bar.Font, b, bounds, sf);
b.Dispose();
inb.Dispose();
}
else
{
g.DrawString(bar.Text, bar.Font, SystemBrushes.WindowText, bounds, sf);
g.SetClip(rect);
g.DrawString(bar.Text, bar.Font, SystemBrushes.HighlightText, bounds, sf);
}
g.SetClip(oldClip, CombineMode.Replace);
if (back != null) back.Dispose();
if (bval != null) bval.Dispose();
}
public void DrawProgressBar(Graphics g, Rectangle bounds, ProgressBar bar)
{
Bitmap back = null, bval = null;
if (rsMgr.Enabeld)
{
back = rsMgr.GetBitmap("PROGRESSBAR"); //背景图片
bval = rsMgr.GetBitmap("PROGRESSBAR_VALUE"); //前景图片
}
//获得比例
decimal v = bar.Value / (decimal)(bar.Maximum - bar.Minimum);
Rectangle rect = bounds;
rect.Inflate(-1, -1);
if (back != null)
{
//绘制背景图片
TextureBrush b = new TextureBrush(back);
b.TranslateTransform(0, rect.Top);
b.ScaleTransform(1, rect.Height / (float)back.Height);
g.FillRectangle(b, rect);
b.Dispose();
ControlPaint.DrawBorder(g, rect, rsMgr.GetColor("BORDERCOLOR"), ButtonBorderStyle.Solid);
}
else
{
//用颜色填充
g.FillRectangle(new SolidBrush(bar.BackColor), rect);
ControlPaint.DrawBorder(g, rect, Color.LightGray, ButtonBorderStyle.Solid);
}
rect.Inflate(-2, -2);
rect.Width = (int)(rect.Width * v);
if (bval != null)
{
//绘制前景图片
TextureBrush b = new TextureBrush(bval);
b.TranslateTransform(0, rect.Top);
b.ScaleTransform(1, rect.Height / (float)bval.Height);
g.FillRectangle(b, rect);
b.Dispose();
}
else
{
//颜色填充
g.FillRectangle(SystemBrushes.Highlight, rect);
}
//输出文本
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Region oldClip = g.Clip;
if (bval != null)
{
//先输出整个文本
Bitmap inb = (Bitmap)back.Clone();
//这里将图片反转
BitmapHelper.Invert(inb);
TextureBrush b = new TextureBrush(inb);
g.DrawString(bar.Text, bar.Font, b, bounds, sf);
//剪辑后在前景范围内输出
g.SetClip(rect);
inb = (Bitmap)bval.Clone();
BitmapHelper.Invert(inb);
b = new TextureBrush(inb);
g.DrawString(bar.Text, bar.Font, b, bounds, sf);
b.Dispose();
inb.Dispose();
}
else
{
g.DrawString(bar.Text, bar.Font, SystemBrushes.WindowText, bounds, sf);
g.SetClip(rect);
g.DrawString(bar.Text, bar.Font, SystemBrushes.HighlightText, bounds, sf);
}
g.SetClip(oldClip, CombineMode.Replace);
if (back != null) back.Dispose();
if (bval != null) bval.Dispose();
}
效果可参见上一文章http://www.cnblogs.com/faib/archive/2009/02/10/1387824.html。