写了一个画单线条的控件,总是不成功,源码如下,希望有时间的人帮忙改下,谢了:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace CCXX.Controls.Windows
{
/// <summary>
/// LineShape 的摘要说明:创建一条横线或竖线
/// </summary>
///
[Description("创建一条横线或竖线")]
public class LineShape : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// 线条颜色
/// </summary>
private Color _BorderColor = new Color();
/// <summary>
/// 线条大小
/// </summary>
///
private int _BorderWidth;
/// <summary>
/// 线条类型
/// </summary>
///
private ELineType _LineType;
#region 属性
[Description("线条颜色"),DefaultValue("Black"),Category("自定义外观")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
[Description("线条大小"),DefaultValue(1),Category("自定义外观")]
public int BorderWidth
{
get
{
return this._BorderWidth;
}
set
{
this._BorderWidth = value;
this.Invalidate();
}
}
[Description("线条类型"),DefaultValue("HLine"),Category("自定义外观")]
public ELineType LineType
{
get
{
return this._LineType;
}
set
{
this._LineType = value;
this.Invalidate();
}
}
#endregion
#region 事件
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
this.DrawLine(e.Graphics);
}
void DrawLine(Graphics g)
{
try
{
g.Clear(this._BorderColor);
Pen p = new Pen(this._BorderColor,this._BorderWidth);
g.DrawLine(p,this.GetPointF1(),this.GetPointF2());
}
finally
{
g.Dispose();
}
}
/// <summary>
/// 线条的第 1 个点的坐标。
/// </summary>
/// <returns></returns>
PointF GetPointF1()
{
PointF p;
p = new PointF(this.Location.X+3,this.Location.Y+3);
return p;
}
/// <summary>
/// 线条的第 2 个点的坐标。
/// </summary>
/// <returns></returns>
PointF GetPointF2()
{
PointF p = new PointF();
switch(this._LineType)
{
///横线
case(ELineType.HLine):
p = new PointF(this.Location.X+3,this.Location.Y+10);
break;
case(ELineType.VLine):
p = new PointF(this.Location.X+10,this.Location.Y+3);
break;
}
return p;
}
#endregion
public LineShape()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// LineShape
//
this.Name = "LineShape";
this._BorderColor = Color.Black;
this._BorderWidth = 1;
this._LineType = ELineType.HLine;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
#endregion
}
}
枚举类:
using System;
namespace CCXX.Controls.Windows
{
/// <summary>
/// 线条的类型:竖线、横线
/// </summary>
public enum ELineType
{
/// <summary>
/// 竖线
/// </summary>
VLine = 1,
/// <summary>
/// 横线
/// </summary>
HLine = 2
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace CCXX.Controls.Windows
{
/// <summary>
/// LineShape 的摘要说明:创建一条横线或竖线
/// </summary>
///
[Description("创建一条横线或竖线")]
public class LineShape : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// 线条颜色
/// </summary>
private Color _BorderColor = new Color();
/// <summary>
/// 线条大小
/// </summary>
///
private int _BorderWidth;
/// <summary>
/// 线条类型
/// </summary>
///
private ELineType _LineType;
#region 属性
[Description("线条颜色"),DefaultValue("Black"),Category("自定义外观")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
[Description("线条大小"),DefaultValue(1),Category("自定义外观")]
public int BorderWidth
{
get
{
return this._BorderWidth;
}
set
{
this._BorderWidth = value;
this.Invalidate();
}
}
[Description("线条类型"),DefaultValue("HLine"),Category("自定义外观")]
public ELineType LineType
{
get
{
return this._LineType;
}
set
{
this._LineType = value;
this.Invalidate();
}
}
#endregion
#region 事件
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
this.DrawLine(e.Graphics);
}
void DrawLine(Graphics g)
{
try
{
g.Clear(this._BorderColor);
Pen p = new Pen(this._BorderColor,this._BorderWidth);
g.DrawLine(p,this.GetPointF1(),this.GetPointF2());
}
finally
{
g.Dispose();
}
}
/// <summary>
/// 线条的第 1 个点的坐标。
/// </summary>
/// <returns></returns>
PointF GetPointF1()
{
PointF p;
p = new PointF(this.Location.X+3,this.Location.Y+3);
return p;
}
/// <summary>
/// 线条的第 2 个点的坐标。
/// </summary>
/// <returns></returns>
PointF GetPointF2()
{
PointF p = new PointF();
switch(this._LineType)
{
///横线
case(ELineType.HLine):
p = new PointF(this.Location.X+3,this.Location.Y+10);
break;
case(ELineType.VLine):
p = new PointF(this.Location.X+10,this.Location.Y+3);
break;
}
return p;
}
#endregion
public LineShape()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// LineShape
//
this.Name = "LineShape";
this._BorderColor = Color.Black;
this._BorderWidth = 1;
this._LineType = ELineType.HLine;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
#endregion
}
}
枚举类:
using System;
namespace CCXX.Controls.Windows
{
/// <summary>
/// 线条的类型:竖线、横线
/// </summary>
public enum ELineType
{
/// <summary>
/// 竖线
/// </summary>
VLine = 1,
/// <summary>
/// 横线
/// </summary>
HLine = 2
}
}
...全文
相关推荐
android画写板涂鸦软件源码
一款android画写,涂鸦功能的软件源码,功能齐全好用,值得学习参考!
MFC Windows 程序设计[二十六]之线条之美(附源码)
MFC是微软公司提供的一个类库(class libraries),用以减少应用程序开发人员的工作量。其中包含大量Windows句柄封装类和很多Windows的内建控件和组件的封装类。有很多初学MFC的小伙伴们都觉得MFC比较难学,看书吧,看了就忘了,网上的一些教程又是写的含糊不清,故而学习起来很吃力...... 故此,本专栏亲自研究,实践,由易到难,层层深入,将调试过程中所遭所遇进行详细讲解,注意事项进行一一列举,希望能够帮助到各位初学MFC的小伙伴,避免走弯路,费时费力。
用c#,vs2005写的仿汽车仪表做的一个仪表控件超酷带源码
用c#,vs2005写的仿汽车仪表做的一个仪表控件超酷带源码
Delphi制作的画流程图的工具(带全部源码)
画流程图的工具,更好地描述你的软件的结构。同时源码中的画图方法也值得你一学。
GDI画尺子.rar
C# winfrom 实现支持,分辨率1*1 有刻度,有源码,下载程序可直接运行查看效果、 C# winfrom 实现支持,分辨率1*1 有刻度,有源码,下载程序可直接运行查看效果


