【发布时间】:2012-03-21 13:25:54
【问题描述】:
我想开发一个标签,以淡入显示类型的方式显示文本。我希望我需要处理这幅画,以便基本上在每次迭代中向右侧添加更多像素。但是,我遇到了障碍,无法让任何动画正常工作。到目前为止,这是我所拥有的:
class RevealLabel : System.Windows.Forms.Label
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
const int GradWidth = 7;
Rectangle r = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y,
GradWidth, e.ClipRectangle.Height);
GraphicsPath p = new GraphicsPath();
using (SolidBrush bs = new SolidBrush(this.ForeColor))
{
using (LinearGradientBrush bg = new LinearGradientBrush(new Rectangle(0, 0, GradWidth, e.ClipRectangle.Height),
this.ForeColor,
this.BackColor,
LinearGradientMode.Horizontal))
{
for (int i = 0; i < e.ClipRectangle.Width; i += GradWidth)
{
r.X = i;
p.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size,
r, StringFormat.GenericDefault);
e.Graphics.FillPath(bg, p);
e.Graphics.Flush();
long TickStop = DateTime.Now.AddSeconds(0.2).Ticks;
while (DateTime.Now.Ticks < TickStop)
{
System.Windows.Forms.Application.DoEvents();
}
e.Graphics.FillPath(bs, p);
}
}
}
e.Graphics.Flush();
}
}
不确定我是否走在正确的轨道上,因为这呈现出的只是一个可怕的混乱。甚至没有逐渐显示到屏幕上的混乱,而是似乎在后台处理所有内容,然后仅使用最终结果更新屏幕。
所以,我的问题有两个:
渲染像素/矩形区域的正确方法是什么?我将在每次迭代时附加到右侧?
如何让它在每次绘制时更新到屏幕上,而不是在完成时只更新一次?
(注意:我也尝试在后台线程中绘制,但一直收到 ArgumentException,我认为是因为 Graphics 对象在离开绘制处理程序方法后不久就退出了可用状态。)
【问题讨论】: