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

bflovesnow 2004-11-10 03:46:34
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
}
}
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fqcd555 2004-12-21
  • 打赏
  • 举报
回复
nga96 2004-12-01
  • 打赏
  • 举报
回复
代码太长了,呵
wntpfi 2004-11-24
  • 打赏
  • 举报
回复
改变控件的大小就可以改变线的长短了
bflovesnow 2004-11-10
  • 打赏
  • 举报
回复
把 第1个的坐标改成 new PointF(0,0)就好了。

不过,怎么能把当前的 Control 的的最小高度、宽度变为 1 呢??

如果能这样的话,我就不需要这样画线啦~~~

如果没有办法的话,偶就结贴啦~~`

谢谢~~
bflovesnow 2004-11-10
  • 打赏
  • 举报
回复
谢谢楼上,现在可以看到线了。

还有个问题就是,怎么让能线的长短随着控件大小的变化而变化??

主要就是 第1点的坐标怎么都定位不好,谢谢 ~~
jimh 2004-11-10
  • 打赏
  • 举报
回复
g.Clear(this.<<<_BorderColor>>>); //这里有问题,怎么刷新背景颜色的跟线条颜色一样了?当然看不到线条。
Pen p = new Pen(this.<<<_BorderColor>>>,this._BorderWidth);
g.DrawLine(p,this.GetPointF1(),this.GetPointF2());
xiaoslong 2004-11-10
  • 打赏
  • 举报
回复
帮你顶
520NET 2004-11-10
  • 打赏
  • 举报
回复
LG

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧