代码来自 网友 loadres
界面预览:

程序源码:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1 Class Form1
Public Shared Function CreateGraphicsPathByControls()Function CreateGraphicsPathByControls(ByVal SourceControl As Control, ByVal TargetControl As Control) As Drawing2D.GraphicsPath
Dim iGraphicsPath As New Drawing2D.GraphicsPath
Dim iSP As Point
Dim iEP As Point
If (TargetControl.Left + TargetControl.Width) < SourceControl.Left Then
'左边
If (TargetControl.Top + TargetControl.Height) < SourceControl.Top Then
'左上角
'|_
'
iSP.X = SourceControl.Left
iSP.Y = SourceControl.Top + SourceControl.Height 4
iEP.X = TargetControl.Left + TargetControl.Width * 3 4
iEP.Y = TargetControl.Top + TargetControl.Height
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X, iSP.Y)
iGraphicsPath.AddLine(iEP.X, iSP.Y, iEP.X, iEP.Y)
ElseIf TargetControl.Top > (SourceControl.Top + SourceControl.Height) Then
'左下角
' _
'|
iSP.X = SourceControl.Left
iSP.Y = SourceControl.Top + SourceControl.Height * 3 4
iEP.X = TargetControl.Left + TargetControl.Width * 3 4
iEP.Y = TargetControl.Top
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X, iSP.Y)
iGraphicsPath.AddLine(iEP.X, iSP.Y, iEP.X, iEP.Y)
Else
'左边
'_
'
iSP.X = SourceControl.Left
iSP.Y = SourceControl.Top + SourceControl.Height 2
iEP.X = TargetControl.Left + TargetControl.Width
iEP.Y = TargetControl.Top + TargetControl.Height 2
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X + (iSP.X - iEP.X) 2, iSP.Y)
iGraphicsPath.AddLine(iEP.X + (iSP.X - iEP.X) 2, iEP.Y, iEP.X, iEP.Y)
End If
ElseIf TargetControl.Left > (SourceControl.Left + TargetControl.Width) Then
'右边
If (TargetControl.Top + TargetControl.Height) < SourceControl.Top Then
'右上角
'_|
'
iSP.X = SourceControl.Left + SourceControl.Width
iSP.Y = SourceControl.Top + SourceControl.Height 4
iEP.X = TargetControl.Left + TargetControl.Width 4
iEP.Y = TargetControl.Top + TargetControl.Height
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X, iSP.Y)
iGraphicsPath.AddLine(iEP.X, iSP.Y, iEP.X, iEP.Y)
ElseIf TargetControl.Top > (SourceControl.Top + SourceControl.Height) Then
'右下角
'_
' |
iSP.X = SourceControl.Left + SourceControl.Width
iSP.Y = SourceControl.Top + SourceControl.Height * 3 4
iEP.X = TargetControl.Left + TargetControl.Width 4
iEP.Y = TargetControl.Top
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X, iSP.Y)
iGraphicsPath.AddLine(iEP.X, iSP.Y, iEP.X, iEP.Y)
Else
'右边
'_
'
iSP.X = SourceControl.Left + SourceControl.Width
iSP.Y = SourceControl.Top + SourceControl.Height 2
iEP.X = TargetControl.Left
iEP.Y = TargetControl.Top + TargetControl.Height 2
iGraphicsPath.AddLine(iSP.X, iSP.Y, iEP.X + (iSP.X - iEP.X) 2, iSP.Y)
iGraphicsPath.AddLine(iEP.X + (iSP.X - iEP.X) 2, iEP.Y, iEP.X, iEP.Y)
End If
Else
If (TargetControl.Top + TargetControl.Height) < SourceControl.Top Then
'上边
'|
'
iSP.X = SourceControl.Left + SourceControl.Width 2
iSP.Y = SourceControl.Top
iEP.X = TargetControl.Left + TargetControl.Width 2
iEP.Y = TargetControl.Top + TargetControl.Height
iGraphicsPath.AddLine(iSP.X, iSP.Y, iSP.X, iSP.Y - (iSP.Y - iEP.Y) 2)
iGraphicsPath.AddLine(iEP.X, iSP.Y - (iSP.Y - iEP.Y) 2, iEP.X, iEP.Y)
ElseIf TargetControl.Top > (SourceControl.Top + SourceControl.Height) Then
'下边
'|
iSP.X = SourceControl.Left + SourceControl.Width 2
iSP.Y = SourceControl.Top + SourceControl.Height
iEP.X = TargetControl.Left + TargetControl.Width 2
iEP.Y = TargetControl.Top
iGraphicsPath.AddLine(iSP.X, iSP.Y, iSP.X, iSP.Y - (iSP.Y - iEP.Y) 2)
iGraphicsPath.AddLine(iEP.X, iSP.Y - (iSP.Y - iEP.Y) 2, iEP.X, iEP.Y)
Else
'重合
'什么都不做
End If
End If
Return iGraphicsPath
End Function
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iG As Graphics = Me.CreateGraphics
Dim iGP As Drawing2D.GraphicsPath
Dim iSPen As New Pen(Color.Blue, 3)
iSPen.StartCap = LineCap.RoundAnchor
iSPen.EndCap = LineCap.ArrowAnchor
iGP = CreateGraphicsPathByControls(ListBox1, ListBox2)
iG.DrawPath(iSPen, iGP)
End Sub
End Class