在下面代码,在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;
}
}
}
}
我已经在构造函数里面加了
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;
}
}
}
}
解决方案 »
- 一个面试题:生成0~100不重复的随机数
- 想在程序中修改电源选项设置
- VB转换成C#时遇到的问题3
- ##########大家有谁做过或知道哪有用.net做的闹钟的程序?##############
- 求救,一个中日问题!
- C# webservice 求助
- treeview读取xml文件,给大家分噢
- 如何最小化窗体到系统托盘,使用什么类,是否需要调用windows API
- emguCv 调用SetCaptureProperty(CapProp.PosFrames, Pos)出错~!
- 需要现打开一个串口即开始每隔5秒向串口发送数据!
- 请问,如何获取tabControl内的其他控件的值(例如TextBox)?
- 使用MSchart显示出图后如何保存图片!
http://www.codeproject.com/cs/media/flickerFreeDrawing.asp
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));
//your draw code
this.ResumeLayout(false);
{
//base.OnPaintBackground(e);
}但在双缓冲里面要绘制背景