画曲线
c
namespace ppt2024
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//存放所有点的位置信息
List<Point> lstPosition = new List<Point>();
//控制开始画的时机
bool isDrawing = false;
//鼠标点击开始画
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
}
//鼠标弹起不画
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
/// <summary>
/// pait 方法不会随时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
//画线
if(lstPosition.Count>1)
{
g.DrawLines(Pens.Pink, lstPosition.ToArray());
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isDrawing)
{
lstPosition.Add(e.Location);
//使得paint方法生效
this.Invalidate();
}
}
}
}
画多条线,不连接
c
namespace ppt2024
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//用集合存放线的位置信息
List<HwFreeLine> lstFreeLine = new List<HwFreeLine>();
//控制开始画的时机
bool isDrawing = false;
//鼠标点击开始画
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
//创建线对象
HwFreeLine freeLine = new HwFreeLine();
//设置线的样式----使用随机函数
Random r = new Random();
freeLine.color = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
freeLine.width = r.Next(1,10);
//集合添加
lstFreeLine.Add(freeLine);
}
//鼠标弹起不画
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
//绘制填充
for(int i=0;i<lstFreeLine.Count;i++)
{
lstFreeLine[i].Draw(g);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isDrawing)
{
//替换掉集合的最后一个点的位置
lstFreeLine[lstFreeLine.Count - 1].lstPoints.Add(e.Location);
//使得paint方法生效
this.Invalidate();
}
}
}
}
画矩形
可以画多个矩形
c
namespace ppt2024
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//存放矩形的位置信息
List<Rectangle> lstRect = new List<Rectangle>();
//控制开始画的时机
bool isDrawing = false;
Rectangle rect;
//鼠标点击开始画
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
rect = new Rectangle();
//矩形起点
rect.X = e.X;
rect.Y = e.Y;
lstRect.Add(rect);
}
//鼠标弹起不画
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
for(int i=0;i<lstRect.Count;i++)
{
g.DrawRectangle(Pens.Blue, lstRect[i]);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isDrawing)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
lstRect[lstRect.Count - 1] = new Rectangle(rect.X, rect.Y, (e.X - rect.X), (e.Y - rect.Y));
//使得paint方法生效
this.Invalidate();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
画带颜色的矩形
c
namespace ppt2024
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//存放矩形的位置信息
List<Rectangle> lstRect = new List<Rectangle>();
//存放矩形填充颜色
Color reactFill = Color.Pink;
//矩形边框颜色
Color reactFrame = Color.Gray;
//矩形边框宽度
int frameSize = 10;
//控制开始画的时机
bool isDrawing = false;
Rectangle rect;
//鼠标点击开始画
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
rect = new Rectangle();
//矩形起点
rect.X = e.X;
rect.Y = e.Y;
lstRect.Add(rect);
}
//鼠标弹起不画
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
//画笔
Pen pen = new Pen(reactFrame, 10);
//纯色画刷
SolidBrush solidBrush = new SolidBrush(reactFill);
//画矩形
for(int i=0;i<lstRect.Count;i++)
{
g.DrawRectangle(pen, lstRect[i]);
}
//绘制填充
for(int i=0;i<lstRect.Count;i++)
{
g.FillRectangle(solidBrush, lstRect[i]);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isDrawing)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
lstRect[lstRect.Count - 1] = new Rectangle(rect.X, rect.Y, (e.X - rect.X), (e.Y - rect.Y));
//使得paint方法生效
this.Invalidate();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
画椭圆
仿造之前的矩形
c
private void Form1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
//画笔
Pen pen = new Pen(reactFrame, 5);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
//纯色画刷
SolidBrush solidBrush = new SolidBrush(reactFill);
//画矩形
for(int i=0;i<lstRect.Count;i++)
{
g.DrawEllipse(pen, lstRect[i]);
}
//绘制填充
for(int i=0;i<lstRect.Count;i++)
{
g.FillEllipse(solidBrush, lstRect[i]);
}
}