在下面代码,在onmousemove这个事件中,如果加了句refresh就会出现闪烁,如何避免这种闪烁的出现呢
我已经在构造函数里面加了
 SetStyle(ControlStyles.UserPaint, true);
 SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
这三个函数,但是还是会闪烁,请大家帮帮忙!using System;
using System.Drawing;
using System.Drawing.Drawing2D; 
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
/// 

public class Form1 : System.Windows.Forms.Form
{
public Graphics g;
public Pen mypen;
public int intx,inty;
public int intwidth,intheight;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(144, 368);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(280, 368);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 16);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(680, 413);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1()); } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{ } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{

} private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
label1.Text=intheight.ToString();
} private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{ } private void Form1_Load(object sender, System.EventArgs e)
{


} private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{ } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
intwidth=e.X-intx;
intheight=e.Y-inty;
label2.Text=intwidth.ToString() + "," +intheight.ToString();
Pen mypen=new Pen(Color.Black,3);
g=CreateGraphics(); 
g.DrawEllipse(mypen,intx,inty,intwidth,intheight);
Refresh(); //如果加这个,就会出现闪烁!

}
} private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
intwidth=e.X-intx;
intheight=e.Y-inty;
Pen mypen=new Pen(Color.Black,3);
g=CreateGraphics(); 
g.DrawEllipse(mypen,intx,inty,intwidth,intheight);
}
} private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
intx=e.X;
inty=e.Y; 
}
}
}
}

解决方案 »

  1.   

    参看
    http://www.codeproject.com/cs/media/flickerFreeDrawing.asp
      

  2.   

    我现在下做一个类似msproject的横道图控件,也有这样的问题,我是使用Invalidate()方法进行了局部刷新,虽然有点闪但是比全部刷新效果要好很多,你可以试试
    int x = startPoint.X < endPoint.X ? startPoint.X : endPoint.X - 20;
    int y = startPoint.Y < endPoint.Y ? startPoint.Y : endPoint.Y - 20;
    int width = Math.Abs(startPoint.X - endPoint.X) + 40;
    int height = Math.Abs(startPoint.Y - endPoint.Y) + 20;
    this.Invalidate(new Rectangle(x, y, width, height));
      

  3.   

    this.SuspendLayout();
    //your draw code
    this.ResumeLayout(false);
      

  4.   

    重载窗体绘制背景的函数:protected override void OnPaintBackground(PaintEventArgs e)
    {
          //base.OnPaintBackground(e);
    }但在双缓冲里面要绘制背景