//控件构造函数     
public GanttChart()
{
            //先绘制位图作为背景
            objBmp = new Bitmap(1280, 1024, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//定义位图
            objGraphics = Graphics.FromImage(objBmp);//绘制图片,作为背景            //无闪烁绘制的设定
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
//重绘方法
 private void PaintChart(Graphics gfx)
{
            DrawMarkEllipse(gfx, rectangleLTPoint, rectanglesideLength, rectanglesideHigh);//绘制标记椭圆           
            objBmp = new Bitmap(this.Width - barStartRight, lastLineStop, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//前两个参数为宽和高
            objGraphics = Graphics.FromImage(objBmp);            
        
}//画椭圆方法定义
private void DrawMarkEllipse(Graphics gfx, Point rectangleleftup, int length, int height)
{
            
            Graphics graphics = this.CreateGraphics();
            Pen  = new Pen(Color.Black, 3);
            Rectangle rectangleside = new Rectangle(rectangleleftup.X, rectangleleftup.Y, length, height);
            graphics.DrawEllipse(, rectangleside);
            
            
 }//双击事件中,调用重绘方法
  protected override void OnMouseDoubleClick(MouseEventArgs e)
{
     PaintChart(gfx);  //我想在控件上双击一下,就在控件相应位置画一个椭圆    
         
}问题是可以画出椭圆,但是我鼠标在控件界面上移动时,椭圆是在不断闪烁(就是一会有,一会无,交替出现,频率很快)??这是怎么回事,我的代码怎么改进可以让他不闪烁???

解决方案 »

  1.   

    绘图放在OnPaint中,老生常谈了先看看MSDN再做
      

  2.   

    PaintChart(gfx);放到OnPaint中,使用OnPaint中e.Graphics绘制,鼠标点击时,Invalidate 重绘
      

  3.   


    我把代码该了,是在鼠标双击的时候重绘,还是闪烁呢?是不是我位图作为背景防止闪烁有问题??//控件构造函数     
    public GanttChart()
    {
                //先绘制位图作为背景
                objBmp = new Bitmap(1280, 1024, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//这个位图自己都没搞懂干嘛的???
                objGraphics = Graphics.FromImage(objBmp);//绘制图片,作为背景????            //无闪烁绘制的设定
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    }protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)//重写OnPaint方法
            {
                base.OnPaint(pe);            PaintChart(pe.Graphics);
            }//重绘时候调用
    public void PaintChart()
            {
                this.Invalidate();//该方法会使得整个控件工作区无效,进行重绘
                
            }
    //重绘方法
     private void PaintChart(Graphics gfx)
    {
                DrawMarkEllipse(gfx, rectangleLTPoint, rectanglesideLength, rectanglesideHigh);//绘制标记椭圆           
                objBmp = new Bitmap(this.Width - barStartRight, lastLineStop, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//这个位图自己都没搞懂干嘛的???
                objGraphics = Graphics.FromImage(objBmp);//这个位图自己都没搞懂干嘛的???            
            
    }//画椭圆方法定义
    private void DrawMarkEllipse(Graphics gfx, Point rectangleleftup, int length, int height)
    {
                
                Graphics graphics = this.CreateGraphics();
                Pen  = new Pen(Color.Black, 3);
                Rectangle rectangleside = new Rectangle(rectangleleftup.X, rectangleleftup.Y, length, height);
                graphics.DrawEllipse(, rectangleside);
                
                
     }//双击事件中,调用重绘方法
      protected override void OnMouseDoubleClick(MouseEventArgs e)
    {
         PaintChart();  //我想在控件上双击一下,就在控件相应位置画一个椭圆    
             
    }问题是可以画出椭圆,但是我鼠标在控件界面上移动时,椭圆是在不断闪烁(就是一会有,一会无,交替出现,频率很快)??这是怎么回事,我的代码怎么改进可以让他不闪烁???
      

  4.   


    我知道哪里错了!哈哈 DrawMarkEllipse方法中的应该用gfx绘图,而不是去自己重新新建private void DrawMarkEllipse(Graphics gfx, Point rectangleleftup, int length, int height)
    {
                
                //Graphics graphics = this.CreateGraphics();
                Pen  = new Pen(Color.Black, 3);
                Rectangle rectangleside = new Rectangle(rectangleleftup.X, rectangleleftup.Y, length, height);
                gfx.DrawEllipse(, rectangleside);
                
                
     }谢谢高手!尤其是2楼了,你帮了我很多次了!