public class LineFrame : System.Windows.Forms.Control { /// <summary> /// 初始化 /// </summary> public LineFrame() { } /// <summary> /// 重绘 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { // TODO: Add custom paint code here Graphics g = pe.Graphics; using(Pen p = new Pen(Color.FromArgb(128, 128, 128))) { g.DrawLine(p, new Point(0, 0), new Point(Width, 0)); } g.DrawLine(Pens.White, new Point(0, 1), new Point(Width, 1)); // Calling the base class OnPaint base.OnPaint(pe); } /// <summary> /// 线条的大小 /// </summary> [DefaultValue(typeof(Size), "75, 2")] protected new Size Size { get { return base.Size; } set { base.Size = value; } } /// <summary> /// 宽 /// </summary> [Browsable(true)] [Category("Layout")] [Description("Gets/Sets the width of the line")] public new int Width { get { return Size.Width; } set { Size = new Size(value, 2); } } //自己再改改,想怎样的Line都行.
谢谢两位,线是画出来了,但我的线始终画不直,等画直了又没法自由拖拽,或通过属性访问器修改(如修改为竖线),一下是全部代码,请帮我看看,我是初学的,thx using System; using System.Drawing; using System.Windows.Forms;namespace MyLine{ public class LineFrame : System.Windows.Forms.Control { /// <summary> /// 初始化 /// </summary> /// private System.Drawing.Pen thePen; protected int x1; protected int y1; protected int x2; protected int y2; public LineFrame() { } /// <summary> /// 重绘 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { //// // Calling the base class OnPaint //// base.OnPaint(pe); thePen = new Pen(System.Drawing.Color.Black );
DrawLine( pe.Graphics ); base.OnPaint( pe ); } #region Line Properties X1 [ System.ComponentModel.Description( "X1" ), System.ComponentModel.Category( "Appearance" ), System.ComponentModel.DefaultValue( null ) ] public int X1 { get { return x1; } set { x1 = value; Invalidate( ); Update( ); } } #endregion #region Line Properties Y1 [ System.ComponentModel.Description( "Y1" ), System.ComponentModel.Category( "Appearance" ), System.ComponentModel.DefaultValue( null ) ] public int Y1 { get { return y1; } set { y1 = value; Invalidate( ); Update( ); } } #endregion #region Line Properties X2 [ System.ComponentModel.Description( "X2" ), System.ComponentModel.Category( "Appearance" ), System.ComponentModel.DefaultValue( null ) ] public int X2 { get { return x2; } set { x2 = value; Invalidate( ); Update( ); } } #endregion #region Line Properties Y2 [ System.ComponentModel.Description( "Y2" ), System.ComponentModel.Category( "Appearance" ), System.ComponentModel.DefaultValue( null ) ] public int Y2 { get { return y2; } set { y2 = value; Invalidate( ); Update( ); } } #endregion protected override void OnSizeChanged( EventArgs e ) {
{
/// <summary>
/// 初始化
/// </summary>
public LineFrame()
{
}
/// <summary>
/// 重绘
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
Graphics g = pe.Graphics;
using(Pen p = new Pen(Color.FromArgb(128, 128, 128)))
{
g.DrawLine(p, new Point(0, 0), new Point(Width, 0));
}
g.DrawLine(Pens.White, new Point(0, 1), new Point(Width, 1)); // Calling the base class OnPaint
base.OnPaint(pe);
}
/// <summary>
/// 线条的大小
/// </summary>
[DefaultValue(typeof(Size), "75, 2")]
protected new Size Size
{
get
{
return base.Size;
}
set
{
base.Size = value;
}
}
/// <summary>
/// 宽
/// </summary>
[Browsable(true)]
[Category("Layout")]
[Description("Gets/Sets the width of the line")]
public new int Width
{
get
{
return Size.Width;
}
set
{
Size = new Size(value, 2);
}
}
//自己再改改,想怎样的Line都行.
using System;
using System.Drawing;
using System.Windows.Forms;namespace MyLine{
public class LineFrame : System.Windows.Forms.Control
{
/// <summary>
/// 初始化
/// </summary>
///
private System.Drawing.Pen thePen; protected int x1;
protected int y1;
protected int x2;
protected int y2; public LineFrame()
{
}
/// <summary>
/// 重绘
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
//// // Calling the base class OnPaint
//// base.OnPaint(pe);
thePen = new Pen(System.Drawing.Color.Black );
DrawLine( pe.Graphics );
base.OnPaint( pe );
}
#region Line Properties X1
[
System.ComponentModel.Description( "X1" ),
System.ComponentModel.Category( "Appearance" ),
System.ComponentModel.DefaultValue( null )
]
public int X1
{
get { return x1; }
set
{
x1 = value;
Invalidate( );
Update( );
}
}
#endregion #region Line Properties Y1
[
System.ComponentModel.Description( "Y1" ),
System.ComponentModel.Category( "Appearance" ),
System.ComponentModel.DefaultValue( null )
]
public int Y1
{
get { return y1; }
set
{
y1 = value;
Invalidate( );
Update( );
}
}
#endregion #region Line Properties X2
[
System.ComponentModel.Description( "X2" ),
System.ComponentModel.Category( "Appearance" ),
System.ComponentModel.DefaultValue( null )
]
public int X2
{
get { return x2; }
set
{
x2 = value;
Invalidate( );
Update( );
}
}
#endregion #region Line Properties Y2
[
System.ComponentModel.Description( "Y2" ),
System.ComponentModel.Category( "Appearance" ),
System.ComponentModel.DefaultValue( null )
]
public int Y2
{
get { return y2; }
set
{
y2 = value;
Invalidate( );
Update( );
}
}
#endregion protected override void OnSizeChanged( EventArgs e )
{
base.OnSizeChanged( e );
this.Invalidate( );
this.Update( );
} protected virtual void DrawLine(Graphics g)
{
g.DrawLine(thePen, x1,y1,ClientSize.Width ,y1 );
}
}
}