Public Declare Function ReleaseCapture Lib "user32" () As LongPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long'Public Const WM_SYSCOMMAND = &H112 'Public Const SC_MOVE = &HF012 Public Const HTCAPTION = 2 Public Const WM_NCLBUTTONDOWN = &HA1If Button = vbLeftButton Then ReleaseCapture SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&End If
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ReleaseCapture SendMessage Me.hwnd, &HA1, HTCAPTION, 0 End Sub
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
'Display the new X,Y position of the form.
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
Private Sub Form1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) mouseIsDown = True cx = X cy = Y End SubPrivate Sub Form1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If mouseIsDown Then '移动窗体 Move Left + (X - cx), Top + (Y - cy) End If End SubPrivate Sub Form1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) mouseIsDown = False End Sub
'Public Const SC_MOVE = &HF012
Public Const HTCAPTION = 2
Public Const WM_NCLBUTTONDOWN = &HA1If Button = vbLeftButton Then
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&End If
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, &HA1, HTCAPTION, 0
End Sub
x1,y1,为单击坐标form1.left=x-x1+form1.left .top=y-y1+.top
要是用api可以看楼上的.
'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
'Display the new X,Y position of the form.
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
mouseIsDown = True
cx = X
cy = Y
End SubPrivate Sub Form1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If mouseIsDown Then
'移动窗体
Move Left + (X - cx), Top + (Y - cy)
End If
End SubPrivate Sub Form1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
mouseIsDown = False
End Sub