1.绘画工具

画笔和画刷 - 画笔Pens:细,画线条和轮廓; - 画刷Brushes:粗,填充闭合区域。

Graphics类 通过 控件名.createGraphics() 取得某控件的屏幕区域作为画布。

2.绘制直线

①取得画布

1
Graphics g = 控件名.CreateGraphics();

②调用g.DrawLine绘制直线

1
g.DrawLine(Pens.Red,10,10,400,300);

说明: - Pens.Red是画笔颜色; - 10,10是起点坐标; - 400,300是终点坐标。

3.填充区域

①Graphics类FillRectangle填充矩形

1
FillRectangle(Brushes.Black,10,10,60,40);

说明: - 其中第一个参数是画刷颜色; - 10,10是矩形左上角顶点坐标; - 60,40是矩形的宽和高,当宽高相同时就是正方形。

②Graphics类FillEllipse填充椭圆

1
FillEllipse(Brushes.Black,10,10,60,40);

说明: - 参数含义与FillRectangle完全相同; - 后4个参数是椭圆外接矩形的坐标和宽高; - 若宽高相同,则是圆形。

4.绘制棋子

鼠标单击事件:MouseClick 获取鼠标位置:e.X, e.Y

5.示例1——同心圆

可用数组定义5个同心圆的颜色。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
        private void button2_Click(object sender, EventArgs e)
        {
            int marrgin = 100;
            Brush []array = new Brush[] {Brushes.Black,Brushes.Blue,Brushes.Chocolate,Brushes.Yellow,Brushes.Red};
            Graphics g = groupBox1.CreateGraphics();
            for (int i = 0; i < 5; i++)
            {
                g.FillEllipse(array[i], marrgin+i*20, marrgin+i*20, 220-i*40, 220-i*40);
            }
            g.Dispose();

        }

6.示例2——棋盘和棋子

要求: - 1.在窗体上画10行10列的棋盘,两条线相隔40像素。 - 2.鼠标单击时在单击位置画一个棋子(直径20的黑圆)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 lianxi3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = groupBox1.CreateGraphics();
            for (int i = 0; i < 10; i++)
            {
                g.DrawLine(Pens.Black, 40, 40 + i * 40, 400, 40 + i * 40);
                g.DrawLine(Pens.Black, 40 + i * 40, 40, 40 + i * 40, 400);
            }
            g.Dispose();
        }
        bool i = true;
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            int n = 30;
            Graphics g = this.CreateGraphics();
            if (i) g.FillEllipse(Brushes.White, e.X - n / 2, e.Y - n / 2, n, n);
            else g.FillEllipse(Brushes.Black, e.X - n / 2, e.Y - n / 2, n, n);
            g.Dispose();
            i = !i;
        }
    }
}

7.示例3——前后颜色和字体

前景色:

1
2
3
4
5
6
7
8
9
        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            DialogResult n = dialog.ShowDialog();
            if (n == DialogResult.OK)
            {
                textBox1.ForeColor = dialog.Color;
            }
        }

背景色:

1
2
3
4
5
6
7
8
9
        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            DialogResult n = dialog.ShowDialog();
            if (n == DialogResult.OK)
            {
                textBox1.BackColor = dialog.Color;
            }
        }

字体格式设置:

1
2
3
4
5
6
7
8
9
        private void button3_Click(object sender, EventArgs e)
        {
            FontDialog dialog = new FontDialog();
            DialogResult n = dialog.ShowDialog();
            if (n == DialogResult.OK)
            {
                textBox1.Font = dialog.Font;
            }
        }