I have a TabControl and a TabPage inside it. BackgroundImage consists of lines attaching the points. So I have some polygon. All these points are kept in the storage. each point has the time property when it was drawn. So I want to repaint the picture using the delays between points. i have the next code
Page pg;
if (storage.book.TryGetValue(currTPage.Name, out pg))
{
currTPage.BackgroundImage = new Bitmap(currTPage.Width, currTPage.Height);
Graphics grap = Graphics.FromImage(currTPage.BackgroundImage);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
foreach (Sequence seq in pg.pageSeq)
{
Dot startDot = null;
Pen pen = new Pen(Color.FromArgb(seq.r, seq.g, seq.b), 1);
foreach (Dot dot in seq.seq)
{
int sX;
int sY;
if (filter.getPageParameters(currentPattern).orientation == Orientation.Landscape)
{
if (this.currTPage.Width / (double)this.currTPage.Height >= 1.4)
{
sX = (int)(dot.x * this.currTPage.Height / pageHeight) + (currTPage.Width - Convert.ToInt32(this.currTPage.Height * Math.Sqrt(2))) / 2;
sY = (int)(dot.y * this.currTPage.Height / pageHeight);
}
else
{
sX = (int)(dot.x * this.currTPage.Width / pageWidth);
sY = (int)(dot.y * this.currTPage.Width / pageWidth) + (currTPage.Height - Convert.ToInt32(this.currTPage.Width / Math.Sqrt(2))) / 2;
}
}
else
{
if (this.currTPage.Width / (double)this.currTPage.Height <= 1 / 1.4)
{
sX = (int)(dot.x * this.currTPage.Width / pageWidth);
sY = (int)(dot.y * this.currTPage.Width / pageWidth) + (currTPage.Height - Convert.ToInt32(this.currTPage.Width * Math.Sqrt(2))) / 2;
}
else
{
sX = (int)(dot.x * this.currTPage.Height / pageWidth) + (currTPage.Width - Convert.ToInt32(this.currTPage.Height / Math.Sqrt(2))) / 2;
sY = (int)(dot.y * this.currTPage.Height / pageWidth);
}
}
if (startDot == null)
{
startDot = new Dot(sX, sY, dot.time, dot.force);
continue;
}
Dot newDot = new Dot(sX, sY, dot.time, dot.force);
grap.DrawLine(pen, startDot.x, startDot.y, newDot.x, newDot.y);
Thread.Sleep((int)(newDot.time - startDot.time));
currTPage.Invalidate(new Rectangle(Math.Min(startDot.x, newDot.x) - 1, Math.Min(startDot.y, newDot.y) - 1, Math.Abs(startDot.x - newDot.x) + 1, Math.Abs(startDot.y - newDot.y) + 1));
startDot = newDot;
}
}
currTPage.Invalidate();
}
but the picture even doesn’t vanish in the beginning of the repainting. it just flash in the end when i do “currTPage.Invalidate();”
What I do wrong?
Your code is fundamentally incompatible with the way painting works in Windows. This code runs on the main thread, like it should, but a thread can do only one thing at a time. It cannot paint the window at the same time it is sleeping or executing your loop. Painting happens after your event handler exits and execution resumes the message loop, the one started by Application.Run(). When there’s nothing more important to do, like handling user input, Windows looks if any part of the window was marked invalid (your Invalidate()) call and generates the Paint event.
You can now probably see what happens, you didn’t implement the Paint event. So it does the default drawing, it erases everything you drew and restores the default appearance of the tab page. The only reason you saw anything at all is because you used Sleep().
You’ll need to completely rewrite this. Use a Timer with an Interval value that is the same as the Sleep(). And a class field that keeps track of the dot index. In the Tick event handler, call Invalidate() and increment the index. And implement the Paint event to do the drawing.