Option Explicit 'Variables 'Switch to turn drah on and off. Dim MoveScreen As Boolean 'Vars to get the mouse position on the form. ' you are draging. Dim MousX As Integer Dim MousY As Integer 'Vars for moving the form. Dim CurrX As Integer Dim CurrY As IntegerPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) MoveScreen = True ' make form movable while the mouse is down. 'Get the initial coordinates of the mouse on the form. MousX = X MousY = Y End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'If the mouse is down on the form then. If MoveScreen Then 'Calculate the new x,y position for the form. ' NB. This is dependant on the X and Y vars on the Form_MouseMove, ' you can use objects MouseMove function. i.e. a Label or Textbox. CurrX = Form1.Left - MousX + X CurrY = Form1.Top - MousY + Y 'Move the form to the new X,Y. Form1.Move CurrX, CurrY ' move form. End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) MoveScreen = False ' stop the form from moving. End Sub
楼上的代码怎么那么多? 不就是一个窗口拖吗?看下面的代码: '****************************************** Option Explicit Dim bDrag As Boolean Dim sngCX As Single, sngCY As SinglePrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) bDrag = True: sngCX = X: sngCY = Y End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If bDrag Then Left = Left + X - sngCX: Top = Top + Y - sngCY End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) bDrag = False End Sub '********************************************
'Variables
'Switch to turn drah on and off.
Dim MoveScreen As Boolean
'Vars to get the mouse position on the form.
' you are draging.
Dim MousX As Integer
Dim MousY As Integer
'Vars for moving the form.
Dim CurrX As Integer
Dim CurrY As IntegerPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveScreen = True ' make form movable while the mouse is down.
'Get the initial coordinates of the mouse on the form.
MousX = X
MousY = Y
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'If the mouse is down on the form then.
If MoveScreen Then
'Calculate the new x,y position for the form.
' NB. This is dependant on the X and Y vars on the Form_MouseMove,
' you can use objects MouseMove function. i.e. a Label or Textbox.
CurrX = Form1.Left - MousX + X
CurrY = Form1.Top - MousY + Y
'Move the form to the new X,Y.
Form1.Move CurrX, CurrY ' move form.
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveScreen = False ' stop the form from moving.
End Sub
不就是一个窗口拖吗?看下面的代码:
'******************************************
Option Explicit
Dim bDrag As Boolean
Dim sngCX As Single, sngCY As SinglePrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
bDrag = True: sngCX = X: sngCY = Y
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If bDrag Then Left = Left + X - sngCX: Top = Top + Y - sngCY
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
bDrag = False
End Sub
'********************************************