下述代码在resize的时候,矩形会有残影,请问如何解决,C#新学,请教了。
-----
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;public class Form2 : Form
{
    internal  class   MyTableControl:   System.Windows.Forms.Panel   
    {   
        private int   itemHeight;
        private int   itemWidth;
        
        public   int   ItemHeight   
        {
            get   {   return   itemHeight;   }   
            set   {   this.itemHeight = value;   }   
        }
        
        public   int   ItemWidth   
        {
            get   {   return   itemWidth;   }   
            set   {   this.itemWidth = value;   }   
        } 
        
        
        public MyTableControl():base()
        {
            SetStyle(ControlStyles.UserPaint,true);   
            SetStyle(ControlStyles.AllPaintingInWmPaint,true);   
            SetStyle(ControlStyles.DoubleBuffer,true);  
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Brush wbrush = new SolidBrush(Color.White);            
            Rectangle TabRect = Rectangle.FromLTRB(this.ClientRectangle.Left,this.ClientRectangle.Top+ItemHeight,this.ClientRectangle.Right-1,this.ClientRectangle.Bottom-1);
            e.Graphics.FillRectangle(wbrush, TabRect);
        }
    }
    private int m_BorderWidth = 4;
    private MyTableControl tableControl;
    
    public Form2()
    {
        tableControl = new MyTableControl();
tableControl.Parent = this;
tableControl.Location = new Point(2*m_BorderWidth,0);
tableControl.ItemHeight =24;
tableControl.ItemWidth = 50;
    }
    
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        tableControl.Size = new Size(ClientRectangle.Right - 4*m_BorderWidth,ClientRectangle.Height -8*m_BorderWidth);
    }
    static void Main() 
    {
        Application.Run(new Form2());
    }
}

解决方案 »

  1.   

                SetStyle(ControlStyles.UserPaint,true);  
                SetStyle(ControlStyles.AllPaintingInWmPaint,true);  
                SetStyle(ControlStyles.DoubleBuffer,true);
     
      

  2.   


    e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), Rec);
    //消除上次画图的痕迹,貌似用Clear()也可以
    Rectangle TabRect = Rectangle.FromLTRB(this.ClientRectangle.Left,this.ClientRectangle.Top+ItemHeight,this.ClientRectangle.Right-1,this.ClientRectangle.Bottom-1); 
    e.Graphics.FillRectangle(wbrush, TabRect); 
      

  3.   

    我个人觉得 Rec,最好设置成  控件.ClientRectangle  .. 
      

  4.   

    还是有的,加上netteans的e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), Rec); 还是存在。
    请实际运行下,呵呵。
      

  5.   

    在Resize里面Refresh(),再看看你的Graphic对象对不对,重画之前填充的矩形大小,再不行,就没天理了,还有,我觉得在Form的Paint事件里画比override OnPaint方法,要好一些个人建议,不必在意,还有问题,欢迎PM,大家一起学习
      

  6.   

    不好意思,我这里resize可能说错了,我的意思是,用户用鼠标改变form的大小时,出现残影,呵呵。请帮忙看看。
      

  7.   

                   BufferedGraphicsContext ctx = BufferedGraphicsManager.Current;
                   BufferedGraphics bg = ctx.Allocate(e.Graphics, new Rectangle(new Point(0, 0), panel1.Size));
                    gOfbuff = bg.Graphics;
    gOfbuff.Draw();
    bg.render
      

  8.   

            private void zhuChart_Paint(object sender, PaintEventArgs e)
            {
                intoGrid();
            }        private void zhuChart_Resize(object sender, EventArgs e)
            {
                Invalidate();
            }在你的paint重绘事件里加上你绘图的方法.
    在你的改变大小里加上上面的Invalidate();就行,你试试
      

  9.   

    intoGrid();//是代表你绘图的方法
      

  10.   

    我的全部代码就在这里(客编译运行),libinguest,可否请你修改之。我没有resize方法,就是当使用鼠标改变大小时就会有残影而已。
      

  11.   

    亦即,我并没有关注resize事件。用户拖动鼠标来改变form大小,我觉得本来就会触发form的paint事件的。
      

  12.   

    呵呵,怪我自己没试,
    protected override void OnResize(EventArgs e)
            {
                this.Invalidate();
            }
    加上libinguest的上述改动即可,呵呵,我还以为默认就会repaint的呢。谁解释下。