c#画直线

代码

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 WindowsFormsApp11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen MyPen = new Pen(Color.Black, 2f);
            //确定起点和终点
            Point pt1 = new Point(70, 20);
            Point pt2 = new Point(200, 320);
            //使用DrawLine方法绘制直线
            GPS.DrawLine(MyPen, pt1, pt2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics GPS = this.CreateGraphics();
            Pen MyPen = new Pen(Color.Red, 2f);
            GPS.DrawLine(MyPen, 50,20,300,200);
        }
    }
}

运行结果

c#画直线_第1张图片
AfterRun.PNG

你可能感兴趣的:(c#画直线)