Dim PrevX As Long, PrevY As LongPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX = X: PrevY = Y
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Text1.Move Text1.Left + X - PrevX, Text1.Top + Y - PrevY
End Sub
以上代码是这里的人教我的一个拖动控件的程序,现在我有两个问题!
1.怎样可以限制拖动的范围,就是不可以把控件拖出我窗口外!2.怎样用鼠标把控件的大小改变.就是好像在VB里改变控件大小的样子!

解决方案 »

  1.   

    你打開VB,新建一個<VB應用程序向導>,在向導中選擇 <MDI多文檔界面>,然后按下一步...完成后就可以運行它看下是怎么實現拖動的了
      

  2.   

    Dim PrevX As Long, PrevY As Long, td As Boolean, yd As Boolean
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If X > Text1.Width - 200 And Y > Text1.Height - 200 Then '仅对右下角处理改变大小
            yd = True
        Else
            td = True
            PrevX = X: PrevY = Y
        End If
    End Sub
    Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        On Error Resume Next
        If Button = 0 Then Exit Sub
        If yd Then Text1.Width = X: Text1.Height = Y
        If td Then Text1.Move Text1.Left + X - PrevX, Text1.Top + Y - PrevY
    End Sub
    Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If td Or yd Then
            If Text1.Left < 0 Then Text1.Left = 0
            If Text1.Top < 0 Then Text1.Top = 0
            If Text1.Left + Text1.Width > Me.ScaleWidth Then Text1.Left = Me.ScaleWidth - Text1.Width
            If Text1.Top + Text1.Height > Me.ScaleHeight Then Text1.Top = Me.ScaleHeight - Text1.Height
            td = False: yd = False
        End If
    End Sub
    当然用MDI窗体比较好