using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 绘制一条直线
Graphics graphics = this.CreateGraphics();//为当前窗体创建Graphics对象
Pen pen1 = new Pen(Color.Red, 6);//实例化一个Pen对象,设定颜色为红色,线条宽度为6
Point startpoint = new Point(20, 320);//设定起点坐标
Point endtpoint = new Point(60, 120);//设定终点坐标
graphics.DrawLine(pen1, startpoint, endtpoint);//调用方法开始绘制一条直线
//graphics.DrawLine(pen1, 20, 320, 60, 120);
//绘制折线
Pen pen2 = new Pen(Color.Blue,3);
Point[] point1 = {
new Point(15,20),
new Point(15,90),
new Point(80,90),
new Point(99,20)
};
graphics.DrawLines(pen2, point1);//调用方法开始绘制多条直线
}
private void button2_Click(object sender, EventArgs e)
{
//绘制多边形
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Yellow, 2);
Point[] point1 = {
new Point(12,90),
new Point(60,80),
new Point(90,20),
new Point(78,150)
};
graphics.DrawPolygon(pen1, point1);//调用DrawPolygon()方法绘制一个空心多变形
Brush brush = new SolidBrush(Color.Black);//实例化一个画刷
Point[] point2 = {
new Point(12,90),
new Point(60,80),
new Point(90,20),
new Point(78,150)
};
graphics.FillPolygon(brush, point2);//调用FillPolygon()方法绘制一个实心多变形
}
private void button3_Click(object sender, EventArgs e)
{
//绘制矩形
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Green, 6);
Brush brush = new SolidBrush(Color.Red);
Rectangle[] rectangle2 = {
new Rectangle(new Point(99, 60), new Size(120, 80)),
new Rectangle(90,120,120,160),
new Rectangle(152,155,120,160)
};
Rectangle rectangle1 = new Rectangle(new Point(10, 60), new Size(60, 80));
graphics.DrawRectangle(pen1, rectangle1);//调用DrawRectangle()方法绘制空心矩形
graphics.DrawRectangle(pen1, 90, 20, 88, 150);
graphics.DrawRectangles(pen1, rectangle2);//调用DrawRectangles()方法绘制多个空心矩形
graphics.FillRectangle(brush, rectangle1);//调用FillRectangle方法绘制一个实心矩形
graphics.FillRectangles(brush, rectangle2); //调用FillRectangles方法绘制多个实心矩形
}
private void button4_Click(object sender, EventArgs e)
{
//绘制椭圆
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Blue);
Rectangle[] rect = {
new Rectangle(188,98,123,188),
new Rectangle(170,110,123,188)
};
Rectangle rectangle1 = new Rectangle(new Point(188, 120), new Size(120, 90));
Brush brush1 = new SolidBrush(Color.Green);
for(int i=0;i<=30;i++)
graphics.DrawEllipse(pen1, 90*i+10, 110 * i + 10,188,130);//通过循环绘制多个空心的椭圆
for (int i = 0; i <= 30; i++)
graphics.FillEllipse(brush1, 90 * i + 10, 110 * i + 10, 188, 130);//通过循环绘制多个实心的椭圆
graphics.DrawEllipse(pen1, rectangle1);//绘制一个空心的椭圆
graphics.FillEllipse(brush1, rectangle1);//绘制一个实心椭圆
}
private void button5_Click(object sender, EventArgs e)
{
//绘制曲线
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Brown,3);
Point[] point = {
new Point(20,140),
new Point(60,10),
new Point(100,130),
new Point(140,20),
new Point(180,130),
new Point(220,30),
new Point(260,120)
};
graphics.DrawCurve(pen1, point, 1.0f);//绘制开口曲线
graphics.DrawClosedCurve(pen1, point);//绘制封闭曲线
}
private void button6_Click(object sender, EventArgs e)
{
//绘制圆弧
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Green, 2);
Rectangle rectangle = new Rectangle(40, 20, 200, 160);
graphics.DrawArc(pen1, rectangle, 210, 90);//绘制圆弧
graphics.DrawArc(pen1, 60,200,180,200, 210, 90);//绘制圆弧
}
private void button7_Click(object sender, EventArgs e)
{
//绘制扇形
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Green, 2);
Brush brush = new SolidBrush(Color.Red);
Rectangle rectangle = new Rectangle(40, 20, 200, 160);
graphics.DrawPie(pen1, rectangle, 210, 90);
graphics.DrawPie(pen1, 60, 200, 180, 200, 210, 90);//绘制一个空心扇形
graphics.FillPie(brush, rectangle, 210, 90);
graphics.FillPie(brush, 60, 200, 180, 200, 210, 90);//绘制一个实心扇形
}
}
}