form1.cs

来自「csharp课本的源代码」· CS 代码 · 共 52 行

CS
52
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DrawLineExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建Graphics对象
            Graphics g = e.Graphics;
            // 创建一支黑色的笔.
            Pen blackPen = new Pen(Color.Black, 3);
            // 定义直线的两个点
            Point point1 = new Point(200,50);
            Point point2 = new Point(400,50);
            // 绘制直线
            g.DrawLine(blackPen, point1, point2);
            // 创建一支红色的笔.
            Pen redPen = new Pen(Color.Red, 3);
            // 定义起点和终点坐标
            int x1 = 200;
            int y1 = 100;
            int x2 = 400;
            int y2 = 130;
            // 绘制直线
            g.DrawLine(redPen, x1, y1, x2, y2);
            // 创建一支蓝色的笔.
            Pen pen = new Pen(Color.Blue , 3);
            // 定义一系列点
            Point[] points =
                {
                     new Point(200,180),
                     new Point(200,200),
                     new Point(400,200),
                     new Point(400,180)
                };
            //绘制直线
            g.DrawLines(pen, points);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?