English Sentence Loading...
英语句子加载中...

在VB程序中用鼠标拖动来改变控件的大小和位置

转载请注明出处

建立如下图所示的窗体,1个label和8个picturebox,picturebox对应的位置在图中已经标明。调整好各个控件的属性值,使之与图中一样。


以下为form1.frm的代码。

Dim Flag As Boolean
Dim MousX As Integer
Dim MousY As Integer
Dim CtrlX As Integer
Dim CtrlY As Integer

Private Sub Form_Load()
     Label1.Caption = Chr(13) & "拖拖拉拉看"
     Picture1.Move Label1.Left, Label1.Top
     Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
     Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
     Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
     Picture5.Move Picture2.Left, Picture4.Top
     Picture6.Move Picture3.Left, Picture4.Top
     Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
     Picture8.Move Picture6.Left, Picture7.Top
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width + MousX - X <= 50 or Label1.Height + MousY - Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture1.Left - MousX + X
         CtrlY = Picture1.Top - MousY + Y
         Picture1.Move CtrlX, CtrlY
         Label1.Move CtrlX, CtrlY, Label1.Width + MousX - X, Label1.Height + MousY - Y
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width <= 50 or Label1.Height + MousY - Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlY = Picture2.Top - MousY + Y
         Picture2.Move Picture2.Left, CtrlY
         Label1.Move Label1.Left, CtrlY, Label1.Width, Label1.Height + MousY - Y
         Picture1.Move Label1.Left, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width - MousX + X <= 50 or Label1.Height + MousY - Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture3.Left - MousX + X
         CtrlY = Picture3.Top - MousY + Y
         Picture3.Move CtrlX, CtrlY
         Label1.Move Label1.Left, CtrlY, Label1.Width - MousX + X, Label1.Height + MousY - Y
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture3_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture4_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width + MousX - X <= 50 or Label1.Height - MousY + Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture4.Left - MousX + X
         CtrlY = Picture4.Top - MousY + Y
         Picture4.Move CtrlX, CtrlY
         Label1.Move CtrlX, Label1.Top, Label1.Width + MousX - X, Label1.Height - MousY + Y
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture4_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture5_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture5_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width <= 50 or Label1.Height - MousY + Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlY = Picture5.Top - MousY + Y
         Picture5.Move Picture5.Left, CtrlY
         Label1.Move Label1.Left, Label1.Top, Label1.Width, Label1.Height - MousY + Y
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture5_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture6_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture6_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width - MousX + X <= 50 or Label1.Height - MousY + Y <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture6.Left - MousX + X
         CtrlY = Picture6.Top - MousY + Y
         Picture6.Move CtrlX, CtrlY
         Label1.Move Label1.Left, Label1.Top, Label1.Width - MousX + X, Label1.Height - MousY + Y
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture6_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture7_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture7_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width + MousX - X <= 50 or Label1.Height <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture7.Left - MousX + X
         Picture7.Move CtrlX, Picture7.Top
         Label1.Move CtrlX, Label1.Top, Label1.Width + MousX - X, Label1.Height
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub Picture7_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub


Private Sub Picture8_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub Picture8_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Label1.Width - MousX + X <= 50 or Label1.Height <= 50 Then
         Flag = False
     End If
     If Flag Then
         CtrlX = Picture8.Left - MousX + X
         Picture8.Move CtrlX, Picture8.Top
         Label1.Move Label1.Left, Label1.Top, Label1.Width - MousX + X, Label1.Height
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
     End If
End Sub

Private Sub Picture8_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub

Private Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
         Flag = True
         MousX = X
         MousY = Y
     End If
End Sub

Private Sub label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Flag Then
         CtrlX = Label1.Left - MousX + X
         CtrlY = Label1.Top - MousY + Y
         Label1.Move CtrlX, CtrlY
         Picture1.Move Label1.Left, Label1.Top
         Picture2.Move Label1.Left + (Label1.Width - Picture2.Width) / 2, Label1.Top
         Picture3.Move Label1.Left + Label1.Width - Picture3.Width, Label1.Top
         Picture4.Move Label1.Left, Label1.Top + Label1.Height - Picture4.Height
         Picture5.Move Picture2.Left, Picture4.Top
         Picture6.Move Picture3.Left, Picture4.Top
         Picture7.Move Label1.Left, Label1.Top + (Label1.Height - Picture7.Height) / 2
         Picture8.Move Picture6.Left, Picture7.Top
     End If
End Sub

Private Sub label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Flag = False
End Sub

运行一下,看看效果。是不是达到目的了?
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: VB 程序 拖动 大小 位置
相关日志:
评论: 0 | 引用: 0 | 查看次数: 3104