自定义控件GDI+绘图闪烁问题,请高手帮忙分析下简化的代码
[code=C#][/code]
//控件构造函数
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 mark = new Pen(Color.Black, 3);
Rectangle rectangleside = new Rectangle(rectangleleftup.X, rectangleleftup.Y, length, height);
graphics.DrawEllipse(mark, rectangleside);
}
//双击事件中,调用重绘方法
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
PaintChart(gfx); //我想在控件上双击一下,就在控件相应位置画一个椭圆
}
问题是可以画出椭圆,但是我鼠标在控件界面上移动时,椭圆是在不断闪烁(就是一会有,一会无,交替出现,频率很快)??这是怎么回事,我的代码怎么改进可以让他不闪烁???
------解决方案--------------------绘图放在OnPaint中,老生常谈了
先看看MSDN再做
------解决方案--------------------
PaintChart(gfx);放到OnPaint中,使用OnPaint中e.Graphics绘制,鼠标点击时,Invalidate 重绘