谁来帮我看看我的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条,第一条就没了,怎么解决这个问题。

解决方案 »

  1.   

    //参考如下代码
    //...namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Bitmap faceBitmap; // 实际绘制线条的位图
            Graphics faceGraphics; // 位图的图像
            Point downPoint = Point.Empty; // 鼠标按下的坐标,如果为Empty则没有按下
            private void Form1_Load(object sender, EventArgs e)
            {
                faceBitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
                faceGraphics = Graphics.FromImage(faceBitmap);
                faceGraphics.FillRectangle(Brushes.White, faceGraphics.ClipBounds);
                DoubleBuffered = true; // 避免闪烁
            }        private void Form1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawImage(faceBitmap, new Point(0, 0));
            }        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;
                downPoint = e.Location;
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (downPoint == Point.Empty) return;
                Graphics vGraphics = Graphics.FromHwnd(Handle);
                vGraphics.DrawImage(faceBitmap, new Point(0, 0));
                vGraphics.DrawLine(Pens.Black, downPoint, e.Location);
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                if (downPoint == Point.Empty) return;
                faceGraphics.DrawLine(Pens.Black, downPoint, e.Location);
                Graphics vGraphics = Graphics.FromHwnd(Handle);
                vGraphics.DrawImage(faceBitmap, new Point(0, 0));
                downPoint = Point.Empty;
            }
        }
    }
      

  2.   

    我的想法是在FROM_PAINT 中画线创建一个Line CLASS把所有画过的线放ARRAYLIST中在Paint中foreach(Line in arrayList)
    {
       Line.Draw(g);
    }
      

  3.   

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