2011-09-24 60 views
1

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

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

我做错了什么?

+0

我认为你试图使错误的矩形失效或我错号? :)如果您没有StartDot,那么您的StartDots和NewDots坐标将会相同。 – BigL

+0

我使用if(startDot == null) startDot = new Dot(sX,sY,dot.time,dot.force); 继续; } ' –

+0

哦,你是对的,我错过了继续; :)不,矩形也没关系,对不起,我错了。 – BigL

回答

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

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

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

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

+0

你是对的,这将是解决方案,我认为这是他正在努力实现的。 PaintEventArgs也有控件图形,所以重绘他的图形会容易得多。 – BigL

0

构建新Graph之后,您并未将其设置为TabPage的新背景。

+0

我一开始就这样做。通过 'currTPage.BackgroundImage =新的位图(currTPage.Width,currTPage.Height); Graphics grap = Graphics.FromImage(currTPage.BackgroundImage); ' 然后我画这个图形。 p.s.currTPage是我想绘制的TabPage –

+0

是的,我看到你将背景设置为一个新的位图,然后从中产生一个带Graphics.FromImage的新图形。 “此方法为指定的图像返回一个新的图形。”然后你在这个图形上绘画,我想如果你准备好了,那么你需要重置这个图形作为你的BackgroundImage的工作。 – BigL