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);
}
}
}
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);
}
}
}
解决方案 »
- 关于C#中适用BITBLT的问题...
- DataGridview问题
- 请问下一下为什么下面的语句不报错
- win2003计划任务,定时执行winform bin目录下的exe,怎么没反应啊?
- GZipStream 流中的read 的最后的100为什么?
- 未将对象引用设置到对象的实例(HttpContext.Current.Server.Execute语句)
- 给个idea吧!在C#中实现将远端的mysql数据库备份到本地。
- 求一條sql語句(自動生成編號)
- DataGrid问题
- 学C#途中遇到问题,请教一下高手!
- 如何用比较简单的办法.查找两组数中.不同的地方
- ( winform )如何实现我的FORM一启动,就能打开一个目录下已经存在的mytest.txt文件,同时打开另存为对话框,让使用者可一存为自己想要的
不行就更改代码:
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();
}
但从你的话中:"我这个是时实的画框的。OnPaint里面就画一次。自然闪不了了啊。"
可能是你没弄好二级缓存吧,显存速度跟不上CPU处理速度
一个front,一个back,这些都是DirectX中的常识也不知道是不是这个问题,仅供参考
这个招不行啊。
一是直接画,同时也自己擦除原来的图。
二是先画到一个BitMap中,在画到屏幕上。
这样的方法不行啊。
我那种方法就是比较闪。
你这种方法。画出来的东西不能清除以前画的东西啊。这样在屏幕上留下的垃圾太多了。
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;
}
-----------------------------------------------------------------------------
在MouseDown,MouseMove,MouseUp事件里刷新PictrueBox就行了。
把你画的给抹了,后你画,又抹掉,就这样重复,能不闪?
我要的就是最后画上去的那个啊。
对了 你说的用 DIRECTDRAW 画。具体怎么用啊。
这里我没有是windowsAPI啊。是不是需要安装微软的DIRECT SDK开发包啊。
这样了结果还是又点闪。各位高人帮帮忙啊。
bool BOOL = false;
Point startPoint = new Point(0, 0);
Point endPoint = new Point(0, 0);
private Pen pen = new Pen(Color.Black); private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
BOOL = false;
} private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (BOOL)
{
endPoint.X = e.X;
endPoint.Y = e.Y;
this.pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (BOOL)
{
Graphics g = e.Graphics;
g.DrawRectangle(pen, startPoint.X, startPoint.Y , endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
}