Reputation: 39
I am making a custom user control , but when i override OnPaint()
, it is not call continuously .
This is my code :
[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("???");
}
}
i waited 10 seconds , message "???" should continuously show up in my Console ,
bug i only see 12 message show up . i tried Invalidate(true);
although message show up continuously , the form is very lag .
e.Graphics.DrawString
is not a very expensive method , right ?
How can i call OnPaint()
continuously without lag?
Upvotes: 0
Views: 1574
Reputation: 5930
Everything in your code works as it should. WinForms is just a framework on top of WinApi and GDI+ so you have to first get some knowledge about windows internal message pump and messages that it sends about which you can read here.
As you can see there's a WM_PAINT
message that is then used by WinForms to repaint the controls.
Every OnPaint
event is called after your application receives WM_PAINT
message. You can of course force this message by using methods like Invalidate()
which will not force painting routine synchronously as stated on msdn page, and you would have to call Update()
after which should be used as :
this.Invalidate();
this.Update();
Or you can directly call Refresh()
method which will force redraw of your control and all of it's children.
Upvotes: 1