Click to See Complete Forum and Search --> : Real-time picturebox painting (ie no Paint event)


robhol
September 12th, 2008, 06:47 AM
Ok.. is there a way to draw in realtime, without this Paint-event crap? I was thinking mainly in conjuction with timer ticks. I have googled and I didn't find anything.

MNovy
September 12th, 2008, 07:21 AM
it is not crap, it is feature you should use.

Maybe your design is not good? Why you want not to use Paint Events?

For Example:
If you move your Window, your stuff must be redrawn -> It's is PaintEvent which does the work.
If you resize your Window, your stuff must be redrawn -> It's is PaintEvent which does the work.

Tip:
Build your own methods drawing some stuff, where the Graphic argument (PaintEventArgs e) is passed to this method. Methods are called in OwnPaint then.

Here is a simple code, which draws a line directly from OwnPaint,
but this can be excluded into other methods:


namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Red, 6), 12, 20, 120, 200);
}
}
}


Use OnPaint, and you will not have to thin k about redrawing stuff....
You can force a refresh/new painting everytime by this.Refresh();