TabPage不重绘背景图像

时间:2011-09-24 13:16:12

标签: c# tabpage

我有一个TabControl和一个TabPage。 BackgroundImage由附加点的线组成。所以我有一些多边形。所有这些点都保存在存储器中。每个点在绘制时都有时间属性。所以我想用点之间的延迟重新绘制图片。我有下一个代码

        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();
        }

但是在重新绘制之初,图片甚至不会消失。当我做“currTPage.Invalidate();”

时,它最终会闪现

我做错了什么?

2 个答案:

答案 0 :(得分:3)

     Thread.Sleep((int)(newDot.time - startDot.time));
     currTPage.Invalidate(new Rectangle(...));

您的代码与绘画在Windows中的工作方式根本不兼容。这段代码在主线程上运行,就像它应该的那样,但一个线程一次只能做一件事。它无法在睡眠或执行循环的同时绘制窗口。在事件处理程序退出后,绘制发生,并且执行恢复消息循环,即Application.Run()启动的消息循环。当没有什么比处理用户输入更重要时,Windows会查看窗口的任何部分是否被标记为无效(您的Invalidate())调用并生成Paint事件。

您现在可能看到会发生什么,您没有实现Paint事件。因此它执行默认绘图,它会删除您绘制的所有内容并恢复标签页的默认外观。你看到任何东西的唯一原因是因为你使用了Sleep()。

你需要完全重写这个。使用具有与Sleep()相同的Interval值的Timer。以及跟踪点索引的类字段。在Tick事件处理程序中,调用Invalidate()并增加索引。并实现Paint事件来进行绘图。

答案 1 :(得分:0)

构建新图表后,您不会将其设置为TabPage的新背景。