using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace AppS
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Thread thDraw;
public bool BOOL=false;
//false是不用画
public Point Lt,Rb;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(480, 389);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 389);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "画图";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=true;
Lt.X=Cursor.Position.X;
Lt.Y=Cursor.Position.Y;
Lt=PointToClient(Lt);
thDraw=new Thread(new ThreadStart(DrawRect));
thDraw.Start();
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=false;
thDraw.Abort();
}
private void DrawRect()
{
while(BOOL)
{
Graphics G=this.pictureBox1.CreateGraphics();
Rb.X=Cursor.Position.X;
Rb.Y=Cursor.Position.Y;
Rb=PointToClient(Rb);
G.DrawRectangle(new Pen(Color.Red,1.0f),Lt.X,Lt.Y,Rb.X-Lt.X,Rb.Y-Lt.Y);
G.Dispose();
this.pictureBox1.Invalidate();
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.GetStyle(ControlStyles.DoubleBuffer);
}
}
}
画图尽量在OnPaint里面写
注释掉this.pictureBox1.Invalidate();试试!
不行就更改代码:
private void DrawRect(Graphics g)
{
while(BOOL)
{
//Graphics G=this.pictureBox1.CreateGraphics();
Rb.X=Cursor.Position.X;
Rb.Y=Cursor.Position.Y;
Rb=PointToClient(Rb);
g.DrawRectangle(new Pen(Color.Red,1.0f),Lt.X,Lt.Y,Rb.X-Lt.X,Rb.Y-Lt.Y);
//G.Dispose();
//this.pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(....)
{
DrawRect(e.Graphics);
}
你应该在PictrueBox.Paint事件里画,然后:
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=true;
Lt.X=Cursor.Position.X;
Lt.Y=Cursor.Position.Y;
Lt=PointToClient(Lt);
thDraw=new Thread(new ThreadStart(DrawRect));
thDraw.Start();
pictureBox1.Refresh();
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=false;
pictureBox1.Refresh();
}
this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
没看你的代码,看代码太烦了,
但从你的话中:"我这个是时实的画框的。OnPaint里面就画一次。自然闪不了了啊。"
可能是你没弄好二级缓存吧,显存速度跟不上CPU处理速度
一个front,一个back,这些都是DirectX中的常识
也不知道是不是这个问题,仅供参考
有两种方法:不能调用Invalidate()方法。
一是直接画,同时也自己擦除原来的图。
二是先画到一个BitMap中,在画到屏幕上。
bool BOOL=false
private void Form1_Load(object sender, System.EventArgs e)
{
this.GetStyle(ControlStyles.DoubleBuffer);
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=true;
Lt.X=Cursor.Position.X;
Lt.Y=Cursor.Position.Y;
Lt=PointToClient(Lt);
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.pictureBox1.Invalidate();
this.pictureBox1.Update();
if(BOOL)
{
Graphics G=this.pictureBox1.CreateGraphics();
Rb.X=Cursor.Position.X;
Rb.Y=Cursor.Position.Y;
Rb=PointToClient(Rb);
G.DrawRectangle(new Pen(Color.Red,1.0f),Lt.X,Lt.Y,Rb.X-Lt.X,Rb.Y-Lt.Y);
G.Dispose();
}
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL=!BOOL;
}
你可以用DIRECTDRAW来实现,兄弟我不知你开个线程的目的是什么,这样就能在后台画?GDI绘图是要先得到句柄的独占权才能会制的,你的那个线程和系统本身的重绘是有冲突的。你的线程在绘图时引起了系统的重绘,把你画的给抹了,后你画,又抹掉,就这样重复,能不闪?
我怎么擦始终擦不干净前一图,带有一些小点。