写了一个画单线条的控件,总是不成功,源码如下,希望有时间的人帮忙改下,谢了:

bflovesnow bflovesnow 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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
}
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
bflovesnow
C# 111,129 成员 642,538 内容