Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Form2.Top = Y + Me.Top Form2.Left = X + Me.Left Form2.Show End Sub
Option ExplicitPrivate Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Form_Click() Dim ptCursor As POINTAPI Dim rcWindow As RECT
GetCursorPos ptCursor Load Form2 Form2.ScaleMode = vbPixels GetWindowRect Form2.hwnd, rcWindow MoveWindow Form2.hwnd, ptCursor.x, ptCursor.y, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, 1 Form2.Show , Me End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Form2.Top = Y + Me.Top Form2.Left = X + Me.Left Form2.Show End Sub
由于VB本身获得的宽度和高度不包括窗口边框和标题栏,所以使用了API。
Ding lyserver不过我觉得没必要用 MoveWindow() 这个 API ,直接调用 Form2.Move 就可以了。 Private Sub Form_Click() Dim ptCursor As POINTAPI Dim rcWindow As RECT
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form2.Top = Y + Me.Top
Form2.Left = X + Me.Left
Form2.Show
End Sub
Option ExplicitPrivate Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Form_Click()
Dim ptCursor As POINTAPI
Dim rcWindow As RECT
GetCursorPos ptCursor
Load Form2
Form2.ScaleMode = vbPixels
GetWindowRect Form2.hwnd, rcWindow
MoveWindow Form2.hwnd, ptCursor.x, ptCursor.y, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, 1
Form2.Show , Me
End Sub
Form2.Top = Y + Me.Top
Form2.Left = X + Me.Left
Form2.Show
End Sub
Private Sub Form_Click()
Dim ptCursor As POINTAPI
Dim rcWindow As RECT
GetCursorPos ptCursor
Load Form2
Form2.ScaleMode = vbPixels
GetWindowRect Form2.hwnd, rcWindow
'MoveWindow Form2.hwnd, ptCursor.x, ptCursor.y, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, 1
Form2.Move ptCursor.X * 15, ptCursor.Y * 15
Form2.Show , Me
End Sub
用这样的代码就行了, API函数: MoveWindow()、GetWindowRect() 都不必用。Private Sub Form_Click()
Dim ptCursor As POINTAPI
Dim rcWindow As RECT
GetCursorPos ptCursor
Load Form2
Form2.Move ptCursor.X * 15, ptCursor.Y * 15
Form2.Show , Me
End Sub