vb图片移动问题
发布网友
发布时间:2023-07-13 14:47
我来回答
共2个回答
热心网友
时间:2023-10-02 20:05
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
LIANXU = False
DoEvents
If LIANXU = False Then
If LIANXU = False Then
If KeyCode = 38 Then
Picture1.Top = Picture1.Top - 10
ElseIf KeyCode = 40 Then
Picture1.Top = Picture1.Top + 10
ElseIf KeyCode = 37 Then
Picture1.Left = Picture1.Left - 10
ElseIf KeyCode = 39 Then
Picture1.Left = Picture1.Left + 10
End If
End If
if picture1.left=picture2.left then
msgbox"hello"
end if
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
LIANXU = True
End Sub
热心网友
时间:2023-10-02 20:06
在移动过程判断是否接触,我给你编了个判断函数ObjHited
'API定义:
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
Private Declare Function IsRectEmpty Lib "user32" (lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'两对象是否接触
Private Function ObjHited(ByVal hwnd1 As Long, ByVal hwnd2 As Long) As Boolean
Dim r1 As RECT
Dim r2 As RECT
Dim rx As RECT
GetWindowRect hwnd1, r1
GetWindowRect hwnd2, r2
IntersectRect rx, r1, r2
If IsRectEmpty(rx) Then
ObjHited = False
Else
ObjHited = True
End If
End Function
'调用方法:
Private Sub Command1_Click()
If ObjHited(Picture1.hwnd, Picture2.hwnd) Then
Debug.Print "Hited!"
Else
Debug.Print "No hited"
End If
End Sub