* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 作 者: 刘镇
* 完成日期: 2012 年 11 月 25 日
* 版 本 号: 3.030
* 对任务及求解方法的描述部分
* 问题描述:创建一个窗体;在窗体上添加一个按钮(text设置为 选择线颜色);向窗体添加一个颜色对话框(colorDialog1)
*代码部分:
Form1.cs:
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 Wind11_2
{
public partial class Form1 : Form
{
Pen pen = null; //定义一个画笔pen
Graphics g = null; //定义一个画布
Point p0, p1; //定义两个坐标点
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pen = new Pen(Color.Black);
g = this.CreateGraphics();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p1 = e.Location;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
p0 = p1;
p1 = e.Location;
g.DrawLine(pen, p0, p1);//实现画线
}
}
private void button1_Click(object sender, EventArgs e)
{
//打开颜色对话框,且获取用户选择颜色
colorDialog1.ShowDialog();
pen.Color = colorDialog1.Color;
}
}
}
测试结果: