0
我正在做一個自定義的用戶控件,但是當我覆蓋OnPaint()
時,它不會連續調用。
這是我的代碼:c#如何連續調用onpaint沒有延遲
[ToolboxData("<{0}:ColoredProgressBar runat=server></{0}:ColoredPorgressBar>")]
public class ColoredProgressBar : ProgressBar
{
public Timer timer;
public ColoredProgressBar()
{
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
}
public void timer_Tick(object sender , EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Call methods of the System.Drawing.Graphics object.
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle);
Console.WriteLine("???");
}
}
我等待10秒,消息 「???」應該不斷顯示在我的控制檯, 錯誤我只看到12消息顯示。我試過Invalidate(true);
雖然消息不斷顯示,但形式卻很滯後。
e.Graphics.DrawString
是不是一個非常昂貴的方法,對不對?
如何連續不斷地撥打OnPaint()
?
那麼你不會導致窗口重繪在該代碼中的任何地方。也許你應該在'timer_Tick()'裏面做一些事情,比如'this.Invalidate()'或'this.Refresh()'...... –
這項工作!謝謝各不相同!^_ ^ – Ben