这个例子是在窗体上左键单击添加选中的控件,右键单击删除选中的控件。即:(有三个控件可以选择:命令按钮、文本框、标签) 当单击左边的任何一个控件,再单击右边的窗体就会出现一个控件,在单击又会出现(数字标记一次增加)当指向添加的控件单击右键时候,就会删除。
设计过程:
Ⅰ、知识要点:
①动态添加控件数组
Load Command1(i)
Command1(i).Left = Text2.Text
Command1(i).Top = Text3.Text
Command1(i).Caption = Str(i)
Command1(i).Visible = True②鼠标事件
③获得当前鼠标位置API函数:GetCursorPos函数
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Ⅱ、设计界面:
Ⅲ、功能代码:
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Type POINTAPI
X As Long
Y As Long
End TypeDim a As Integer
Dim b As Integer
Dim c As IntegerPrivate Sub Command1_Click(Index As Integer)
a = 1
End SubPrivate Sub Command1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And Index > 0 Then
Unload Command1(Index) '右键删除命令按钮
End If
End SubPrivate Sub Command2_Click()
End
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim mouse As POINTAPI
Dim i As Integer
GetCursorPos mouse
b = Val(mouse.X) * 15 - Me.Left - 400
c = Val(mouse.Y) * 15 - Me.Top - 700
If b > 1000 Then
If a = 1 Then
If Button = 1 Then
i = Command1.UBound + 1
Load Command1(i)
Command1(i).Left = b
Command1(i).Top = c
Command1(i).Caption = Str(i)
Command1(i).Visible = True
End If
End If
If a = 2 Then
If Button = 1 Then
i = Label1.UBound + 1
Load Label1(i)
Label1(i).Left = b
Label1(i).Top = c
Label1(i).Caption = Str(i)
Label1(i).Visible = True
End If
End If
If a = 3 Then
If Button = 1 Then
i = Text1.UBound + 1
Load Text1(i)
Text1(i).Left = b
Text1(i).Top = c
Text1(i).Visible = True
End If
End If
End If
End SubPrivate Sub Label1_Click(Index As Integer)
a = 2
End SubPrivate Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And Index > 0 Then
Unload Label1(Index) '右键删除标签
End If
End SubPrivate Sub Text1_Click(Index As Integer)
a = 3
End SubPrivate Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And Index > 0 Then
Unload Text1(Index) '右键删除文本框
End If
End Sub