我在利用vc#.net画图的时候遇到了问题,我想画一条直线,也成功了,但不是我想要的效果。我想要的效果是:直线画上去之后,如果不删除就不要从picturebox画布中消除。但是现在每次在画新的图形之前,以前的直线就消除了.请问大家有没有什么解决办法private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.x1 = e.X;
this.y1 = e.Y;
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.pen = new Pen(Color.Red);
if(e.Button == MouseButtons.Left)
{
this.g.Clear(this.pictureBox1.BackColor);
this.x2 = e.X;
this.y2 = e.Y; g.DrawLine(pen,this.x1,this.y1,this.x2,this.y2); }
{
this.x1 = e.X;
this.y1 = e.Y;
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.pen = new Pen(Color.Red);
if(e.Button == MouseButtons.Left)
{
this.g.Clear(this.pictureBox1.BackColor);
this.x2 = e.X;
this.y2 = e.Y; g.DrawLine(pen,this.x1,this.y1,this.x2,this.y2); }
这个语句在窗口上画了一条直线,注意:只画了一次。
如果窗口被刷新(有很多情况都会导致窗口刷新),这条直线必须由你自己的代码重新绘制出来。
窗口是不会记忆自己原先有什么东西、在刷新后又把它们画上去的。如果你的软件是象实现类似paint那样的画图程序,你应该记录下鼠标移动轨迹上的点,然后每次窗口刷新后,根据记录的这些点,把这些线重新绘制出来
1.在MouseDown中定义起始点;
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
x1 = e.X;
y1 = e.Y;
x2 = e.X;
y2 = e.Y;
}
2.MouseMove中,删除前面画的线,重画现在的;
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
g.DrawLine( new Pen( Color.White ),this.x1,this.y1,this.x2,this.y2);//"White" is your back color
this.x2 = e.X;
this.y2 = e.Y;
g.DrawLine( new Pen( Color.Red ),this.x1,this.y1,this.x2,this.y2);
}
}
3.在MouseUp,保存线的节点信息,以供Paint事件中用;
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
g.DrawLine( new Pen( Color.White ),this.x1,this.y1,this.x2,this.y2);//"White" is your back color
this.x2 = e.X;
this.y2 = e.Y;
g.DrawLine( new Pen( Color.Red ),this.x1,this.y1,this.x2,this.y2); //Save it into buffer here}
}
因为你在处理以前直线的时候是利用画布背景色填充的,这样虽然red的线看不到了,只是利用背景色掩盖了
不知道我的意思是不是表达清楚了,不过还是谢谢“愚翁”了
谁还有更好的方法,可以来交流下哦
因为你在处理以前直线的时候是利用画布背景色填充的,这样虽然red的线看不到了,只是利用背景色掩盖了
不知道我的意思是不是表达清楚了,不过还是谢谢“愚翁”了
谁还有更好的方法,可以来交流下哦
——————————————————————————————————————觉得你表达的不够清楚,不用背景色掩盖还能怎样?
这个不知道符合你的要求不。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace TestDoubleBuffer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 271);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
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); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} Point pt1 = new Point(0, 0);
Point pt2 = new Point(0, 0);
Color m_cl = Color.Blue;
bool blCatched = false;
System.Collections.ArrayList al = new System.Collections.ArrayList(); private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
pt1.X = e.X;
pt1.Y = e.Y;
blCatched = true;
} private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (blCatched)
{
using (Graphics g = this.CreateGraphics())
{
using(Pen p = new Pen(this.BackColor))
{
g.DrawLine(p, pt1, pt2);
using(PaintEventArgs arg = new PaintEventArgs(g, ClientRectangle))
{
Form1_Paint(this, arg);
pt2.X = e.X;
pt2.Y = e.Y;
p.Color = Color.Blue;
g.DrawLine(p, pt1, pt2);
p.Dispose();
}
}
}
}
} private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
al.Add(new line(pt1.X,pt1.Y,pt2.X,pt2.Y,m_cl));
blCatched = false;
using (Graphics g = this.CreateGraphics())
{
using (PaintEventArgs arg = new PaintEventArgs(g, ClientRectangle))
{
Form1_Paint(this, arg);
}
}
} private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
foreach (object obj in al)
{
(obj as line).Draw(e.Graphics);
}
} protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground (pevent);
Form1_Paint(this,pevent);
} } public class line
{
private Point m_pt1 = new Point(0, 0);
private Point m_pt2 = new Point(0, 0);
private Color m_cl = Color.Black;
public line(int x1,int y1,int x2,int y2,Color cl)
{
m_pt1.X = x1;
m_pt1.Y = y1;
m_pt2.X = x2;
m_pt2.Y = y2;
m_cl = cl;
}
public void Draw(Graphics g)
{
using (Pen p = new Pen(m_cl))
{
g.DrawLine(p, m_pt1, m_pt2);
}
}
}
}