MSDN中定义Pen.DashPaten 获取或者设置下划线的短划线的长度和空白区域的长度。

例如:

float[] Pts = { 3,1,2,5};

p2.DashStyle = DashStyle.Dash;     

p2.DashPattern = Pts; 就是指画短划线的时候,第一笔长3个单位,空白1个单位,接下来长2个单位,再空5个单位,如此循环。

测试代码如下:

 

 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Drawing.Drawing2D;  
  10. namespace _003点_直线和曲线  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         protected override void OnPaint(PaintEventArgs e)  
  19.         {  
  20.             //base.OnPaint(e);  
  21.             Graphics G = e.Graphics;    // 构造Graphics对象  
  22.             Pen p1 = new Pen(Color.Blue,10);    // 实例化Pen对象  
  23.             G.DrawLine(p1,20,20,330,20);        // 画直线  
  24.  
  25.             Pen p2 = new Pen(Color.Blue,2);    // 实例化Pen对象  
  26.             float[] Pts = { 3,1,2,5};           // 定义一个浮点型数组  
  27.             p2.DashStyle = DashStyle.Dash;      // 定义Pen p2的DashStyle类型为DashStye  
  28.             p2.DashPattern = Pts;  
  29.                     
  30.             G.DrawLine(p2,20,50,330,50);  
  31.         }  
  32.     }  
  33. }  

结果如下: