using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WMS.Ctrl { public partial class LineX : UserControl { public LineX() { InitializeComponent(); this.Name = "LineX"; this.lineWidth = 5; this.lineColor = Color.Green; this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.Opaque, true); this.BackColor = Color.Transparent; this.Paint += new PaintEventHandler(LineX_Paint); this.Resize += new EventHandler(LineX_Resize); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle = 0x20; return cp; } } #region 定义属性 private Color lineColor; /// /// 定义线的颜色 /// public 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; } } private int lineWidth; /// /// 定义线的粗细 /// 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; } } private int lStartCap; public int LStartCap { set { this.lStartCap = value; System.Windows.Forms.PaintEventArgs ep = new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle); this.LineX_Paint(this, ep); } get { return this.lStartCap; } } private int lEndCap; public int LEndCap { set { this.lEndCap = value; System.Windows.Forms.PaintEventArgs ep = new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle); this.LineX_Paint(this, ep); } get { return this.lEndCap; } } #endregion /// /// 控件调整大小时发生的事件 /// /// /// private void LineX_Resize(object sender, System.EventArgs e) { this.Height = (this.LineWidth * 2); } /// /// 控件重绘时发生的事件 /// /// /// private void LineX_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Pen p = new Pen(this.LineColor, this.LineWidth);//设置笔的粗细为,颜色为蓝色 Graphics g = this.CreateGraphics(); if (LEndCap == 1) { p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头 } else { p.EndCap = LineCap.NoAnchor; //p.DashStyle = DashStyle.Solid;//恢复实线 } if (LStartCap == 1) { p.StartCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头 } else { p.StartCap = LineCap.NoAnchor; //p.DashStyle = DashStyle.Solid;//恢复实线 } g.DrawLine(p, 0, (this.LineWidth), e.ClipRectangle.Right, (this.LineWidth)); this.Height = (this.LineWidth * 2); g.Dispose(); p.Dispose(); } } }