日期:2014-05-19  浏览次数:20887 次

画直线小程序需要改进,来看看吧
谁来帮我看看我的C#小程序
  悬赏分:20   -   离问题结束还有   14   天   6   小时
编了一个画直线的小程序,我的思想是这样的,点了画直线按钮后,再按下鼠标就开始画线,不要松开鼠标键拖动的过程中会产生一条直线。松开鼠标后直线确定。  
using   System;  
using   System.Collections.Generic;  
using   System.ComponentModel;  
using   System.Data;  
using   System.Drawing;  
using   System.Text;  
using   System.Windows.Forms;  
namespace   DrawLine  
{  
public   class   Form1   :   Form  
{  
bool   stmove=false;//判断MouseMove操作是否开始  
int   x1,y1,x2,y2;//声明线的起点和终点坐标  
bool   start=false;  
Graphics   g;  
Button   btn;  
Pen   pen;  
public   Form1()  
{  
this.SuspendLayout();  
pen   =   new   Pen(Color.Red,   2);  
btn   =   new   Button();  
btn.Text   =   "画直线 ";  
this.Controls.Add(btn);  
this.ResumeLayout();  
btn.Click   +=   new   EventHandler(btn_Click);  
this.MouseDown   +=   new   MouseEventHandler(Form1_MouseDown);  
this.MouseUp   +=   new   MouseEventHandler(Form1_MouseUp);  
this.MouseMove   +=   new   MouseEventHandler(Form1_MouseMove);  
}  
private   void   btn_Click(object   sender,EventArgs   e)  
{  
start   =   true;  
g   =   this.CreateGraphics();  
}  
private   void   Form1_MouseDown(object   sender,MouseEventArgs   e)  
{  
if(start)  
{  
stmove   =   true;  
x1=e.X;  
y1=e.Y;  
}  
}  
private   void   Form1_MouseMove(object   sender,   MouseEventArgs   e)  
{  
if   (stmove)  
{  
g.Clear(this.BackColor);  
x2   =   e.X;  
y2   =   e.Y;  
g.DrawLine(pen,   x1,   y1,   x2,   y2);  
}  
}  
private   void   Form1_MouseUp(object   sender,MouseEventArgs   e)  
{  
if(start)  
{  
y2   =   e.Y;  
x2   =   e.X;  
g.DrawLine(pen,x1,y1,x2,y2);  
stmove   =   false;  
}  
}  
static   void   Main()  
{  
Application.EnableVisualStyles();  
Application.SetCompatibleTextRenderingDefault(false);  
Application.Run(new   Form1());  
}  
}  
}  
我发现一个问题就是画完第一调直线后再画第2条,第一条就没了,怎么解决这个问题。

------解决方案--------------------
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}

private ArrayList drawingList = new ArrayList();
private Line currentLine = null;
private void Form5_MouseDown(object sender, MouseEventArgs e)
{
currentLine = new Line(new Point(e.X, e.Y));
}

private void Form5_MouseUp(object sender, MouseEventArgs e)
{
if (currentLine != null)
{
currentLine.MouseMove(new Po