C# 未清除先前油漆的模板

C# 未清除先前油漆的模板,c#,winforms,C#,Winforms,我正在尝试覆盖Windows窗体的绘制事件,但是我对窗体所做的所有绘制都将保留,即使在我对窗体执行了Invalidate()和Update()之后也是如此 我正在使用一个计时器来Invalidate()和Update()表单,这会导致调用OnPaint() 代码如下: // In the constructor the timer is created and enabled private void UpdaterElapsed(object sender, System.Timers.Ela

我正在尝试覆盖Windows窗体的绘制事件,但是我对窗体所做的所有绘制都将保留,即使在我对窗体执行了
Invalidate()
Update()
之后也是如此

我正在使用一个计时器来
Invalidate()
Update()
表单,这会导致调用
OnPaint()

代码如下:

// In the constructor the timer is created and enabled
private void UpdaterElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    WIDGET.Invalidate();
    WIDGET.Update();
}
private void WIDGET_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    String text = DateTime.Now.ToString("hh:mm:ss tt");
    e.Graphics.DrawString(text, new Font("Arial", 32), Brushes.Black, new Point(0, 0));
}

它不应该清除窗体(尽管通过覆盖窗口来触发绘制事件应该可以完成这项工作)。如果需要,只需自行清除即可:

e.Graphics.Clear(/* insert your color here */);

你必须用一些应该被视为透明的颜色来清除图形,然后只需设置透明键

e.Graphics.Clear(Color.Purple);
this.TransparencyKey = Color.Purple;

控件的类型是什么
小部件
e.Graphics.Clear(您的背景色)清除任何内容不是绘制事件的任务。OnPaintBackground()执行此操作时,将应用背景色。非常不清楚为什么它不能完成任务,你的代码片段没有用。这是根据@HansPassant所说的MCVE闭包。或者,从另一个角度,使用适当的
事件
问题是我想使用的背景色是color.Transparent。这画成了黑色。@KFCChicken也许你的问题应该提到这一点。您的帖子下面有一个编辑链接。@KFCChicken,那么您有两个案例。您实际上指的是窗口背景系统颜色(在这种情况下,请查看
SystemColors
类),或者您希望在背景上绘制它,在这种情况下,您可以使用
Graphics.DrawImage
来“清除”它。问题是,如果文本的颜色为紫色,那么它也会消失,否?@kfchicken当为TransparencyKey属性指定颜色时,具有相同背景色的窗体区域将透明显示@莱尔,这就是问题所在。无论设置了什么颜色,透明键都不能用于绘图目的。