2010-10-05

小畫板

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
 public partial class FormPainter : Form
 {
  Graphics g;
  Pen pen;

  public FormPainter()
  {
   InitializeComponent();
   g = this.CreateGraphics();
   pen = new Pen(Color.Black, 3);

  }
   bool isMouseDown = false;// 一開始設MouseDown為0

   List<Point> points = new List<Point>();// 宣告一個陣列,放產生出來的值

  private void Form1_MouseDown(object sender, MouseEventArgs e)
  {
   isMouseDown = true;// 驅動MouseDown
   points.Add(new Point(e.X, e.Y));// 紀錄點的位子

  }

  private void Form1_MouseMove(object sender, MouseEventArgs e)
  {
   if (isMouseDown)// 畫出所經過的筆順
   {
    points.Add(new Point(e.X, e.Y));
    g.DrawLine(pen, points[points.Count - 2], points[points.Count - 1]);
   }

  }

  private void Form1_MouseUp(object sender, MouseEventArgs e)
  {
   points.Add(new Point(e.X, e.Y));
   g.DrawLine(pen, points[points.Count - 2], points[points.Count - 1]);
   points.Add(new Point(-1, -1));// 讓電腦知道斷點的地方,設一個不可能的值(電腦不會出現-1的值)
   isMouseDown = false;

  }

  private void FormPainter_Load(object sender, EventArgs e)
  {

  }
 }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License