如何在表单中绘制一条线来连接vb6中的两个对象

时间:2014-06-18 06:58:03

标签: vb6

我正在制作流程图程序。我做了所有这些,但我遇到了这个问题。 如何通过获取鼠标的位置在visual basic中的两个按钮之间动态绘制一条线(连接线)!!

所以这是它的代码。我做了如何获得光标的位置,但我无法前进。

Option Explicit 

Private Type POINTAPI
    x As Long 
    y As Long 
End Type 

Private Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long 

Dim z As POINTAPI 

Private Sub Form_Load() 
    Timer1.Interval = 1 
    Timer1.Enabled = True 
End Sub 

Private Sub Timer1_Timer() 
    GetCursorPos z 
    Label1 = "x: " & z.x 
    Label2 = "y: " & z.y 
End Sub

请帮我们解决这个问题! 提前致谢

1 个答案:

答案 0 :(得分:1)

为什么需要鼠标的位置?

查看以下测试项目:

'1 form with:
'  2 command buttons: name=Command1  name=Command2
Option Explicit

Private Sub ConnectButtons(cmd1 As CommandButton, cmd2 As CommandButton)
  Dim sngX1 As Single, sngX2 As Single
  Dim sngY1 As Single, sngY2 As Single
  With cmd1
    sngX1 = .Left + .Width
    sngY1 = .Top + .Height / 2
  End With 'cmd1
  With cmd2
    sngX2 = .Left
    sngY2 = .Top + .Height / 2
  End With 'cmd2
  Line (sngX1, sngY1)-(sngX2, sngY2)
End Sub

Private Sub Form_Click()
  ConnectButtons Command1, Command2
End Sub

Private Sub Form_Resize()
  Command1.Move 120, 120
  Command2.Move ScaleWidth / 2, ScaleHeight / 2
End Sub

运行时,它会在表单上显示2个命令按钮,当您单击表单时,它将绘制连接线

调整表单大小以更改Command2的位置,然后再次单击该表单

注意传递给ConnectButtons子

的命令按钮的顺序