Graphics.DrawLine()方法用於繪製連接由坐標對指定的兩個點的線。此方法的重載列表中有4種方法,如下所示:
- DrawLine(Pen, PointF, PointF) Method
- DrawLine(Pen, Int32, Int32, Int32, Int32) Method
- DrawLine(Pen, Single, Single, Single, Single) Method
- DrawLine(Pen, Point, Point) Method
在這裏,我們將討論前兩種方法。
DrawLine(Pen, PointF, PointF) Method
此方法用於繪製從一組指定的點到一組指定的點的線形。它需要一個PointF變量,該變量由(x,y)點組成。
用法:
public void DrawLine (System.Drawing.Pen pen, System.Drawing.PointF pt1, System.Drawing.PointF pt2);
參數:
- pen:筆確定線條的顏色,寬度和樣式。
- pt1:將(x,y)坐標定義為初始點的PointF變量。
- pt2:將(x,y)坐標定義為最終點的PointF變量。
異常:如果pen為null,則此方法將提供ArgumentNullexception。
例:
// C# program to demonstrate the
// DrawLine(Pen, PointF, PointF) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm:Form {
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
// Defines pen
Pen pen = new Pen(ForeColor);
// Defines the both points to connect
// pt1 is (30.0, 30.0) which represents (x1, y1)
PointF pt1 = new PointF(30.0F, 30.0F);
// pt2 is (200.0, 300.0) which represents (x2, y2)
PointF pt2 = new PointF(200.0F, 300.0F);
// Draws the line
pea.Graphics.DrawLine(pen, pt1, pt2);
}
}
}
輸出:
DrawLine(Pen, Int32, Int32, Int32, Int32) Method
此方法用於繪製線形式的一組指定坐標,這些坐標以離散形式x1,y1,x2,y2給出。
用法:
public void DrawLine (System.Drawing.Pen pen, int x1, int y1, int x2, int y2);
參數:
- pen:Pen確定線條的顏色,寬度和樣式。
- x1:第一點的橫坐標。
- y1:第一點的縱坐標。
- x2:第二點的橫坐標。
- y2:第二點的縱坐標。
異常:如果pen為null,則此方法將提供ArgumentNullexception。
範例1:
// C# program to draw a cross using the
// DrawLine(Pen, Int32, Int32, Int32,
// Int32) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm:Form {
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
// Defines the pen
Pen pen = new Pen(ForeColor);
// To draw a cross we need to make 2
// diagonals from top-left to the
// bottom-right and top-right to the
// bottom-left to calculate these
// coordinates we would take help of
// our screen size top-left = (0, 0)
// bottom-right = (ClientSize.Width - 1,
// ClientSize.Height - 1)
// top-right = (ClientSize.Width - 1, 0)
// bottom-left = (0, ClientSize.Height - 1)
pea.Graphics.DrawLine(pen, 0, 0, ClientSize.Width - 1,
ClientSize.Height - 1);
pea.Graphics.DrawLine(pen, ClientSize.Width - 1, 0, 0,
ClientSize.Height - 1);
}
}
}
輸出:
範例2:
// C# program to Create a 8x8 square board
// using // DrawLine(Pen, Int32, Int32, Int32,
// Int32) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm:Form {
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
// Define Pen
Pen pen = new Pen(ForeColor);
// loop for all 7 horizontal lines to draw
for (int i = 0; i < 7; i++)
{
// Taking Block size to be 30x30
// So width and height will be 30*8=240
pea.Graphics.DrawLine(pen, i * 30, 0, i * 30, 240);
}
// loop for all 7 horizontal lines to draw
for (int i = 0; i < 7; i++)
{
pea.Graphics.DrawLine(pen, 0, i * 30,
240, i * 30);
}
}
}
}
輸出:
相關用法
- C# Math.Max()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自ShivamChauhan5大神的英文原創作品 C# | Graphics.DrawLine() Method | Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。