主题:VB自动绘制直线端点垂直线段的代码
我们绘图时常常需要在直线的端点自动绘制垂直线段(以下简称端线),这事看起来简单,编起程来竟然还有点复杂,经过一天的冥思苦想和反复实验,终于获得成功!
新建一个窗体,上面只放置一个Line控件。代码如下:
Option Explicit
Dim editX As Single '画线时鼠标的初始X坐标
Dim editY As Single '画线时鼠标的初始Y坐标
Dim BasePos As Integer '端线位置:0-无端线,1-鼠标按下端,2-鼠标抬起端,3-两端均有
Private Sub Form_Load()
Line1.Visible = False
DrawWidth = 3
Line1.BorderWidth = DrawWidth
ScaleMode = 3
AutoRedraw = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 38: BasePos = IIf(BasePos = 0, 2, IIf(BasePos = 3, 1, IIf(BasePos = 2, 0, 3))) '↑单次按此键在鼠标抬起端画端线,双次按取消
Case 40: BasePos = IIf(BasePos = 0, 1, IIf(BasePos = 3, 2, IIf(BasePos = 1, 0, 3))) '↓单次按此键在鼠标按下端画端线,双次按取消
End Select
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
editX = X: editY = Y
Line1.X1 = X: Line1.Y1 = Y: Line1.Visible = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X2 = X: Line1.Y2 = Y
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.Visible = False
Line (editX, editY)-(X, Y), 0
If BasePos Then DrawBase editX, editY, X, Y
End Sub
Private Sub DrawBase(X1!, Y1!, X2!, Y2!)
Dim LineL% '直线长
Dim BaseLine% '端线半长
Dim bx!, by!, sLX!, sLY!, sRX!, sRY!
BaseLine = 20 '取值范围控制在5-100之间
LineL = Sqr(((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2)) '由勾股定理求直线长
Select Case BasePos '画端线
Case 1: GoSub 400 '在鼠标按下端画端线
Case 2: GoSub 500 '在鼠标抬起端画端线
Case 3: GoSub 400: GoSub 500 '两端都画端线
End Select
Exit Sub
400
bx = X1 + (X2 - X1) / LineL / 10
by = Y1 + (Y2 - Y1) / LineL / 10
sLX = bx + BaseLine * (Y1 - by) * 10
sLY = by - BaseLine * (X1 - bx) * 10
sRX = bx - BaseLine * (Y1 - by) * 10
sRY = by + BaseLine * (X1 - bx) * 10
Line (sLX, sLY)-(sRX, sRY), 0
Return
500
bx = X2 - (X2 - X1) / LineL / 10
by = Y2 - (Y2 - Y1) / LineL / 10
sLX = bx + BaseLine * (Y2 - by) * 10
sLY = by - BaseLine * (X2 - bx) * 10
sRX = bx - BaseLine * (Y2 - by) * 10
sRY = by + BaseLine * (X2 - bx) * 10
Line (sLX, sLY)-(sRX, sRY), 0
Return
End Sub
简要说明:
在画直线前,单次按【↑】键,在鼠标抬起端画端线,双次按取消;单次按【↓】键,在鼠标按下端画端线,双次按取消。如果此两键都按下,则在直线两端都画端线。
新建一个窗体,上面只放置一个Line控件。代码如下:
Option Explicit
Dim editX As Single '画线时鼠标的初始X坐标
Dim editY As Single '画线时鼠标的初始Y坐标
Dim BasePos As Integer '端线位置:0-无端线,1-鼠标按下端,2-鼠标抬起端,3-两端均有
Private Sub Form_Load()
Line1.Visible = False
DrawWidth = 3
Line1.BorderWidth = DrawWidth
ScaleMode = 3
AutoRedraw = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 38: BasePos = IIf(BasePos = 0, 2, IIf(BasePos = 3, 1, IIf(BasePos = 2, 0, 3))) '↑单次按此键在鼠标抬起端画端线,双次按取消
Case 40: BasePos = IIf(BasePos = 0, 1, IIf(BasePos = 3, 2, IIf(BasePos = 1, 0, 3))) '↓单次按此键在鼠标按下端画端线,双次按取消
End Select
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
editX = X: editY = Y
Line1.X1 = X: Line1.Y1 = Y: Line1.Visible = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X2 = X: Line1.Y2 = Y
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.Visible = False
Line (editX, editY)-(X, Y), 0
If BasePos Then DrawBase editX, editY, X, Y
End Sub
Private Sub DrawBase(X1!, Y1!, X2!, Y2!)
Dim LineL% '直线长
Dim BaseLine% '端线半长
Dim bx!, by!, sLX!, sLY!, sRX!, sRY!
BaseLine = 20 '取值范围控制在5-100之间
LineL = Sqr(((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2)) '由勾股定理求直线长
Select Case BasePos '画端线
Case 1: GoSub 400 '在鼠标按下端画端线
Case 2: GoSub 500 '在鼠标抬起端画端线
Case 3: GoSub 400: GoSub 500 '两端都画端线
End Select
Exit Sub
400
bx = X1 + (X2 - X1) / LineL / 10
by = Y1 + (Y2 - Y1) / LineL / 10
sLX = bx + BaseLine * (Y1 - by) * 10
sLY = by - BaseLine * (X1 - bx) * 10
sRX = bx - BaseLine * (Y1 - by) * 10
sRY = by + BaseLine * (X1 - bx) * 10
Line (sLX, sLY)-(sRX, sRY), 0
Return
500
bx = X2 - (X2 - X1) / LineL / 10
by = Y2 - (Y2 - Y1) / LineL / 10
sLX = bx + BaseLine * (Y2 - by) * 10
sLY = by - BaseLine * (X2 - bx) * 10
sRX = bx - BaseLine * (Y2 - by) * 10
sRY = by + BaseLine * (X2 - bx) * 10
Line (sLX, sLY)-(sRX, sRY), 0
Return
End Sub
简要说明:
在画直线前,单次按【↑】键,在鼠标抬起端画端线,双次按取消;单次按【↓】键,在鼠标按下端画端线,双次按取消。如果此两键都按下,则在直线两端都画端线。