我这里是画直线和画曲线的两种都有,楼主看看,错不了的,代码如下:
namespace Case02_1 {
public partial class Form1 : Form
{
int startX; //获取鼠标起始点的X坐标
int startY; //获取鼠标起始点的Y坐标
Graphics g; //定义Graphics对象实例
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.Snow;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Red , 4);
if (radioButton1.Checked == true)
{
g.DrawLine(p, startX, startY, e.X, e.Y);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startX = e.X;
startY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Blue , 2);
if (radioButton2.Checked == true)
{
g.DrawRectangle(p, e.X, e.Y, 1, 1);
}
}
private void button1_Click(object sender, EventArgs e)
{
g = this.CreateGraphics();
g.Clear(Color.Snow );
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
转自:http://zhidao.baidu.com/question/256505194.html