Graphics.DrawLine 方法

命名空间:System.Drawing

程序集:System.Drawing.dll, System.Drawing.Common.dll

功能:绘制一条连接由坐标对指定的两个点的线条。

1、DrawLine(Pen, PointF, PointF)

功能绘制一条连接两个 PointF(浮点) 结构的线。

参数

           第一个参数Pen,它确定线条的颜色、宽度和样式。

           第二个参数PointF它表示要连接的第一个点。

           第三个参数PointF它表示要连接的第二个点。

说明DrawLine(Pen, Point, Point)功能及用法同上,只是数据类型不同,PointFPoint

示例

         //创建Graphics对象,GPS局部变量

         Graphics GPS = this.CreateGraphics();

        //创建pen局部变量blackpen

        Pen blackPen = new Pen(Color.Black, 3);

        // 创建点的坐标

        PointF point1 = new PointF(100.0F, 100.0F);

        PointF point2 = new PointF(500.0F, 100.0F);

        //画线

        GPS.DrawLine(blackPen, point1, point2);

2、DrawLine(Pen, Int32, Int32, Int32, Int32)

功能:绘制一条连接由坐标对指定的两个点的线条。

参数

           第一个参数Pen,它确定线条的颜色、宽度和样式。

           第二个参数Int32,第一个点的 x 坐标。

           第三个参数Int32,第一个点的 y 坐标。

           第四个参数Int32,第二个点的 x 坐标。

           第五个参数Int32,第二个点的 y 坐标。

说明DrawLine(Pen, Single, Single, Single, Single)功能及用法同上,只是数据类型不同,Int32Single

示例

         //创建Graphics对象,GPS局部变量

        Graphics GPS = this.CreateGraphics();

        //创建pen局部变量bluepen

         Pen bluePen = new Pen(Color.Blue, 6);

         // 创建点的坐标

         int x1 = 100;

         int y1 = 300;

         int x2 = 500;

         int y2 = 300;

         // 画线,此处x1, y1, x2, y2直接输入数字100,300,500,300,则无需创建点的坐标

         GPS.DrawLine(bluePen, x1, y1, x2, y2);

 

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DoubleBufferDraw { public partial class DrawLine : Form { class LineObj { private Point m_start; private Point m_end; public LineObj(Point start, Point end) { this.m_start = start; this.m_end = end; } public void Draw(Graphics g, Pen pen) { g.DrawLine(pen, m_start, m_end); } } private Point m_startPoint = Point.Empty; List lineList = new List(); public DrawLine() { InitializeComponent(); } private void drawLine(Graphics graphics, Point startPoint, Point endPoint) { BufferedGraphicsContext context = BufferedGraphicsManager.Current; BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle); bg.Graphics.Clear(this.BackColor); foreach (LineObj line in this.lineList) { line.Draw(bg.Graphics, SystemPens.ControlText); } bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint); bg.Render(); bg.Dispose(); bg = null; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); foreach (LineObj line in this.lineList) { line.Draw(e.Graphics, SystemPens.ControlText); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_startPoint = new Point(e.X, e.Y); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y)); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); LineObj line = new LineObj(this.m_startPoint, e.Location); this.lineList.Add(line); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器人自动化控制

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值