C#畫線控件的開發(fā)應(yīng)用實例解析
作者:freetest
C#畫線控件是什么呢?是如何使用的呢?那么這里向你介紹了C#畫線控件開發(fā)中的兩種,分別是橫線和豎線的實現(xiàn),希望對你了解和學(xué)習(xí)C#畫線控件有所幫助。
C#畫線控件的應(yīng)用實例介紹之前我們要明白在C#中沒有畫線的控件,這里寫了一個,大家分享。共有兩個控件分別是畫橫線和畫豎線的,關(guān)于怎么畫斜線我還沒沒有,有興趣的可以做一個大家分享。
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;}
- }
- ///
- /// 線的粗細(xì)
- ///
- 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;
- ///
- /// 構(gòu)造函數(shù)初始顏色和線粗細(xì)
- ///
- 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 組件設(shè)計器生成的代碼
- ///
- /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
- /// 修改此方法的內(nèi)容。
- ///
- 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;}
- }
- ///
- /// 線的粗細(xì)
- ///
- 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;
- ///
- /// 構(gòu)造函數(shù)初始顏色和線粗細(xì)
- ///
- 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 組件設(shè)計器生成的代碼
- ///
- /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
- /// 修改此方法的內(nèi)容。
- ///
- 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#畫線控件的開發(fā)就向你介紹到這里,希望對你了解和學(xué)習(xí)C#畫線控件有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
cnblogs