首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

画直线小程序需要改进,来看看吧,该如何解决

2012-01-16 
画直线小程序需要改进,来看看吧谁来帮我看看我的C#小程序悬赏分:20-离问题结束还有14天6小时编了一个画直

画直线小程序需要改进,来看看吧
谁来帮我看看我的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 Point(e.X,e.Y));
drawingList.Add(currentLine);


currentLine = null;
this.Invalidate();
}
}

private void Form5_MouseMove(object sender, MouseEventArgs e)
{
if (currentLine != null && e.Button == MouseButtons.Left)
{
currentLine.MouseMove(new Point(e.X, e.Y));
this.Invalidate();

}
}

private void Form5_Paint(object sender, PaintEventArgs e)
{
foreach (Line line in drawingList)
{
line.Draw(e.Graphics);
}
if (currentLine != null)
{
currentLine.Draw(e.Graphics);
}
}
}
public class Line
{
private Point startPoint = new Point(0,0);
private Point endPoint= new Point(0,0);
public void MouseMove(Point p)
{
endPoint = p;
}
public Line(Point p)
{
startPoint = p;
}
public void Draw(Graphics g)
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawLine(Pens.Black, startPoint, endPoint);
}
}

热点排行