定义一个集合存储线的信息 dim lines as arraylist 定义一个线的类,存储每条线的信息当添加线的时候不直接画,先添加到集合中在form的paint事件中把集合中的线都画出来,或定义一个控件类,重写onpaint事件
我的线的类 Imports System.Drawing.Drawing2D Public Class MyLine Public Sub New() Me.M_Pen = New Pen(Color.Black, 1.5!) Me.M_Pen.DashStyle = DashStyle.Solid End Sub Public StartLabel As Winfar_Label Public EndLabel As Winfar_Label Public FocusFlag As Boolean '是否选中 Public DelayCol As Integer Public DelayMargin As Single Public bz As String Public DrawArrowFlag As Boolean Public ArrowLength As Integer = 10 Public Property Visible() As Boolean Get Return M_Visible And Me.StartLabel.Visible And Me.EndLabel.Visible End Get Set(ByVal Value As Boolean) Me.M_Visible = Value End Set End Property Public KeyPathFlag As Boolean Public StartX As Double = 1 Public StartY As Double = 2 / 3 Public EndX As Double = 0 Public EndY As Double = 2 / 3 Public Property Color() As Color Get Return Me.M_Pen.Color End Get Set(ByVal Value As Color) Me.M_Pen.Color = Value End Set End Property Public Property Width() As Single Get Return Me.M_Pen.Width End Get Set(ByVal Value As Single) Me.M_Pen.Width = Value End Set End Property Public Property DashStyle() As DashStyle Get Return Me.M_Pen.DashStyle End Get Set(ByVal Value As DashStyle) Me.M_Pen.DashStyle = Value End Set End Property Friend ReadOnly Property Pen() As Pen Get Return Me.M_Pen End Get End Property Private M_Pen As Pen Private M_Visible As Boolean = True End Class
dim lines as arraylist
定义一个线的类,存储每条线的信息当添加线的时候不直接画,先添加到集合中在form的paint事件中把集合中的线都画出来,或定义一个控件类,重写onpaint事件
Imports System.Drawing.Drawing2D
Public Class MyLine
Public Sub New()
Me.M_Pen = New Pen(Color.Black, 1.5!)
Me.M_Pen.DashStyle = DashStyle.Solid
End Sub
Public StartLabel As Winfar_Label
Public EndLabel As Winfar_Label
Public FocusFlag As Boolean '是否选中
Public DelayCol As Integer
Public DelayMargin As Single
Public bz As String
Public DrawArrowFlag As Boolean
Public ArrowLength As Integer = 10
Public Property Visible() As Boolean
Get
Return M_Visible And Me.StartLabel.Visible And Me.EndLabel.Visible
End Get
Set(ByVal Value As Boolean)
Me.M_Visible = Value
End Set
End Property
Public KeyPathFlag As Boolean Public StartX As Double = 1
Public StartY As Double = 2 / 3
Public EndX As Double = 0
Public EndY As Double = 2 / 3 Public Property Color() As Color
Get
Return Me.M_Pen.Color
End Get
Set(ByVal Value As Color)
Me.M_Pen.Color = Value
End Set
End Property
Public Property Width() As Single
Get
Return Me.M_Pen.Width
End Get
Set(ByVal Value As Single)
Me.M_Pen.Width = Value
End Set
End Property
Public Property DashStyle() As DashStyle
Get
Return Me.M_Pen.DashStyle
End Get
Set(ByVal Value As DashStyle)
Me.M_Pen.DashStyle = Value
End Set
End Property
Friend ReadOnly Property Pen() As Pen
Get
Return Me.M_Pen
End Get
End Property
Private M_Pen As Pen
Private M_Visible As Boolean = True
End Class
代码如下:
public class MyControl:Control
{
public MyControl()
{
this.bigFont = new Font("宋体", 21.75f);
this.smFont = new Font("宋体", 12f); this.textbox1 = new TextBox();
this.textbox2 = new TextBox(); this.textbox1.Multiline = true;
this.textbox2.Multiline = true;
this.textbox1.Font = this.textbox2.Font = new Font("宋体", 18); this.Controls.Add(textbox1);
this.Controls.Add(textbox2); this.TextBoxWidth = 12;
} TextBox textbox1, textbox2;
public string AddressFrom { get; set; }
public Color AddressFromColor { get; set; }
public string AddressTo { get; set; }
public Color AddressToColor { get; set; }
public string UpInfo { get; set; }
public string DownInfo { get; set; }
int distance=20;
public int TextBoxDistance
{
get { return this.distance; }
set
{
if (value >= 20)
{
this.distance = value;
updateWidth();
}
}
}
int textboxWidth=60;
public int TextBoxWidth
{
get { return this.textboxWidth; }
set
{
if (value >= 60)
{
this.textboxWidth = value;
updateWidth();
}
}
}
float addressFromWidth, addressToWidth, upInfoWidth, downInfoWidth, lineWidth;
Font bigFont, smFont; void updateWidth()//计算宽度
{
Graphics g = this.CreateGraphics(); this.addressFromWidth = g.MeasureString(this.AddressFrom, this.bigFont).Width;
this.addressToWidth = g.MeasureString(this.AddressTo, this.bigFont).Width;
this.upInfoWidth = g.MeasureString(this.UpInfo, this.smFont).Width;
this.downInfoWidth = g.MeasureString(this.DownInfo, this.smFont).Width;
this.lineWidth = Math.Max(upInfoWidth, downInfoWidth); this.Width = (int)(addressFromWidth + lineWidth + addressToWidth + 4 + 2 * this.TextBoxWidth + this.TextBoxDistance+6);
this.textbox1.Left = (int)(addressFromWidth + lineWidth + addressToWidth + 4);
this.textbox2.Left = this.Width - this.TextBoxWidth;
this.textbox1.Top = this.textbox2.Top = 0;
this.textbox1.Width = this.textbox2.Width = this.textboxWidth;
this.textbox1.Height = this.textbox2.Height = this.Height;
} protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e); if (this.Height != 32)
this.Height = 32;
} protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; float start = 0; e.Graphics.DrawString(this.AddressFrom, bigFont, new Pen(this.AddressFromColor).Brush, start, 3);
start += this.addressFromWidth + 4;
e.Graphics.DrawString(this.UpInfo, smFont, Brushes.SteelBlue, start, 0);
e.Graphics.DrawString(this.DownInfo, smFont, Brushes.SteelBlue, start, this.Height / 2 + 1); e.Graphics.DrawLine(new Pen(Color.Violet) { EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor,Width=2 },
new Point((int)start-2, this.Height / 2),
new Point((int)(start + lineWidth), this.Height / 2)); start += lineWidth + 2; e.Graphics.DrawString(this.AddressTo, bigFont, new Pen(this.AddressToColor).Brush, (int)start, 3); start += addressToWidth+ 2; e.Graphics.DrawLine(new Pen(Color.Black) { Width = 2 },
new Point((int)start, 1),
new Point((int)start, this.Height - 1)); start += this.textboxWidth; e.Graphics.DrawLine(new Pen(Color.Green) { EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor, Width = 2 },
new Point((int)start, this.Height / 2),
new Point((int)start + this.distance, this.Height / 2));
}
}
new Point((int)start-2, this.Height / 2),
new Point((int)(start + lineWidth), this.Height / 2));
红色地方出错(我用的VS2005),能不解释一下?谢谢~
共有30个错误(都是和红色的地方有关系-全是应该输入‘)’或是无效‘,’或是‘;’
new Pen(Color.Violet) { EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor,Width=2 }
把这句拿出来.
写Pen pen = new Pen(Color.Violet);
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
pen.Width = 2;
e.Graphics.DrawLine(pen,
new Point((int)start-2, this.Height / 2),
new Point((int)(start + lineWidth), this.Height / 2));
其他的类似.
还有自动属性get;set;
改为:
int a;
public int A
{
get{return a;}
set{a=value;}//设置了出发地,目的地,控件的宽度要重新算一下。请调用updateWidth();
}