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);
}
}
}

解决方案 »

  1.   

    我这个是时实的画框的。OnPaint里面就画一次。自然闪不了了啊。
      

  2.   

    注释掉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();
    }
      

  3.   

    this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
      

  4.   

    没看你的代码,看代码太烦了,
    但从你的话中:"我这个是时实的画框的。OnPaint里面就画一次。自然闪不了了啊。"
    可能是你没弄好二级缓存吧,显存速度跟不上CPU处理速度
    一个front,一个back,这些都是DirectX中的常识也不知道是不是这个问题,仅供参考
      

  5.   

    jinjazz(近身剪(充电中...)) 
    这个招不行啊。
      

  6.   

    有两种方法:不能调用Invalidate()方法。
    一是直接画,同时也自己擦除原来的图。
    二是先画到一个BitMap中,在画到屏幕上。
      

  7.   

    3tzjq(永不言弃) ( ) 信誉:110 
    这样的方法不行啊。
    我那种方法就是比较闪。
    你这种方法。画出来的东西不能清除以前画的东西啊。这样在屏幕上留下的垃圾太多了。
      

  8.   

    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;
    }
      

  9.   

    在mouse  move的时候才重绘
      

  10.   

    你这种方法。画出来的东西不能清除以前画的东西啊。这样在屏幕上留下的垃圾太多了。
    -----------------------------------------------------------------------------
    在MouseDown,MouseMove,MouseUp事件里刷新PictrueBox就行了。
      

  11.   

    你可以用DIRECTDRAW来实现,兄弟我不知你开个线程的目的是什么,这样就能在后台画?GDI绘图是要先得到句柄的独占权才能会制的,你的那个线程和系统本身的重绘是有冲突的。你的线程在绘图时引起了系统的重绘,把你画的给抹了,后你画,又抹掉,就这样重复,能不闪?
      

  12.   

    tslkfyh(TSL) ( ) 信誉:100 
     把你画的给抹了,后你画,又抹掉,就这样重复,能不闪?
    我要的就是最后画上去的那个啊。
    对了 你说的用 DIRECTDRAW 画。具体怎么用啊。
    这里我没有是windowsAPI啊。是不是需要安装微软的DIRECT SDK开发包啊。
      

  13.   

    我到是可以擦干净。但是还是又写闪啊。能不能坐的想WINDOWS中的鼠标拖动一样啊。这样还是比较闪啊。
      

  14.   

    我是在鼠标MOVE的时候画的啊。但是还是闪啊。
      

  15.   

    this.GetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint);
    这样了结果还是又点闪。各位高人帮帮忙啊。
      

  16.   

    这样画一点都不闪
    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);
    }
    }