Option ExplicitPrivate Const HIT_CAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HIT_CAPTION, 0
End Sub
Private Const WM_NCLBUTTONDOWN = &HA1Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HIT_CAPTION, 0
End Sub
先用getcursor() [api]取得当前的鼠标位置,再把me.top=mouse.y:me.left=mouse.x
注意,getcursor() 返回的是像素值!
fX = X
fY = Y
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button > 0 Then Me.Move Me.Left - fX + X, Me.Top - fY + Y
End Sub
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate blnIsDown As Boolean
Private blnIsCtrlDown As Boolean
Private xpoint As POINTAPIPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then
blnIsCtrlDown = True
End If
End SubPrivate Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then
blnIsCtrlDown = False
End If
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
MouseManange x, y
blnIsDown = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
MouseManange x, y
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
MouseManange x, y
If blnIsCtrlDown Then
If blnIsDown Then
GetCursorPos xpoint
Me.Top = xpoint.y * 15
Me.Left = xpoint.x * 15
blnIsDown = False
End If
End If
End Sub
Private Sub MouseManange(x As Single, y As Single)
If blnIsCtrlDown Then
If GetCapture() <> Me.hWnd Then SetCapture Me.hWnd
ElseIf ((x < 0) Or (y < 0) Or (x > Me.Width) Or (y > Me.Height)) Then
ReleaseCapture
End If
End Sub