C#畫線控制元件的示例程式碼

iDotNetSpace發表於2009-12-29

  C#畫線控制元件的應用例項介紹之前我們要明白在C#中沒有畫線的控制元件,這裡寫了一個,大家分享。共有兩個控制元件分別是畫橫線和畫豎線的,關於怎麼畫斜線有興趣的可以做一個大家分享。

  C#畫線控制元件之橫線

 using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Windows.Forms;
  namespace Jiashi.WinControls
  {
  ///
  /// LineX 畫橫線控制元件
  ///
  public class LineX : System.Windows.Forms.UserControl
  {
  #region 屬性定義
  private System.Drawing.Color lineColor;
  private int lineWidth;
  ///
  /// 線的顏色屬性
  ///
  public System.Drawing.Color LineColor
  {
  set
  {
  this.lineColor=value;
  System.Windows.Forms.PaintEventArgs ep=
  new PaintEventArgs(this.CreateGraphics(),
  this.ClientRectangle);
  this.LineX_Paint(this,ep);
  }
  get{return this.lineColor;}
  }
  ///
  /// 線的粗細
  ///
  public int LineWidth
  {
  set
  {
  this.lineWidth=value;
  System.Windows.Forms.PaintEventArgs ep=
  new PaintEventArgs(this.CreateGraphics(),
  this.ClientRectangle);
  this.LineX_Paint(this,ep);
  }
  get{return this.lineWidth;}
  }
  #endregion
  private System.ComponentModel.Container components = null;
  ///
  /// 建構函式初始顏色和線粗細
  ///
  public LineX()
  {
  InitializeComponent();
  this.lineColor=this.ForeColor;
  this.lineWidth=1;
  }
  ///
  /// 清理所有正在使用的資源。
  ///
  protected override void Dispose( bool disposing )
  {
  if( disposing )
  {
  if(components != null)
  {
  components.Dispose();
  }
  }
  base.Dispose( disposing );
  }
  #region 元件設計器生成的程式碼
  ///
  /// 設計器支援所需的方法 - 不要使用程式碼編輯器
  /// 修改此方法的內容。
  ///
  private void InitializeComponent()
  {
  //
  // LineX
  //
  this.Name = "LineX";
  this.Resize += new System.EventHandler(this.LineX_Resize);
  this.Paint +=
  new System.Windows.Forms.PaintEventHandler(this.LineX_Paint);
  }
  #endregion
  private void LineX_Paint(object sender,
  System.Windows.Forms.PaintEventArgs e)
  {
  Graphics g=e.Graphics;
  Pen myPen = new Pen(this.lineColor);
  myPen.Width=this.lineWidth*2;
  this.Height=this.LineWidth;
  g.DrawLine(myPen,0,0,e.ClipRectangle.Right,0);
  }
  private void LineX_Resize(object sender, System.EventArgs e)
  {
  this.Height=this.lineWidth;
  }
  }
  }

  C#畫線控制元件之豎線

 using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Windows.Forms;
  namespace Jiashi.WinControls
  {
  ///
  /// LineY 畫豎線控制元件
  ///
  public class LineY : System.Windows.Forms.UserControl
  {
  #region 屬性定義
  private System.Drawing.Color lineColor;
  private int lineWidth;
  ///
  /// 線的顏色屬性
  ///
  public System.Drawing.Color LineColor
  {
  set
  {
  this.lineColor=value;
  System.Windows.Forms.PaintEventArgs ep=
  new PaintEventArgs(this.CreateGraphics(),
  this.ClientRectangle);
  this.LineY_Paint(this,ep);
  }
  get{return this.lineColor;}
  }
  ///
  /// 線的粗細
  ///
  public int LineWidth
  {
  set
  {
  this.lineWidth=value;
  System.Windows.Forms.PaintEventArgs ep=
  new PaintEventArgs(this.CreateGraphics(),
  this.ClientRectangle);
  this.LineY_Paint(this,ep);
  }
  get{return this.lineWidth;}
  }
  #endregion
  private System.ComponentModel.Container components = null;
  ///
  /// 建構函式初始顏色和線粗細
  ///
  public LineY()
  {
  InitializeComponent();
  this.lineColor=this.ForeColor;
  this.lineWidth=1;
  }
  ///
  /// 清理所有正在使用的資源。
  ///
  protected override void Dispose( bool disposing )
  {
  if( disposing )
  {
  if(components != null)
  {
  components.Dispose();
  }
  }
  base.Dispose( disposing );
  }
  #region 元件設計器生成的程式碼
  ///
  /// 設計器支援所需的方法 - 不要使用程式碼編輯器
  /// 修改此方法的內容。
  ///
  private void InitializeComponent()
  {
  //
  // LineY
  //
  this.Name = "LineY";
  this.Resize +=
  new System.EventHandler(this.LineY_Resize);
  this.Paint +=
  new System.Windows.Forms.PaintEventHandler(this.LineY_Paint);
  }
  #endregion
  private void LineY_Paint(
  object sender, System.Windows.Forms.PaintEventArgs e)
  {
  Graphics g=e.Graphics;
  Pen myPen = new Pen(this.lineColor);
  myPen.Width=this.lineWidth*2;
  this.Width=this.LineWidth;
  g.DrawLine(myPen,0,0,0,e.ClipRectangle.Bottom);
  }
  private void LineY_Resize(
  object sender, System.EventArgs e)
  {
  this.Width=this.lineWidth;
  }
  }
  }

  C#畫線控制元件的開發就向你介紹到這裡,希望對你瞭解和學習C#畫線控制元件有所幫助。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-623873/,如需轉載,請註明出處,否則將追究法律責任。

相關文章