绘制控件间的连接线

本文介绍了一种使用Visual Basic .NET在两个控件间绘制连线的方法。通过判断控件的位置关系,自动调整连线形状,确保连线清晰可见。适用于窗体设计中需要直观展示控件间逻辑关系的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码来自 网友 loadres

界面预览:

程序源码:

 

Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1

    
Public Shared 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(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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值