'取得窗体位置的函数
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
'取得鼠标位置的函数
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'鼠标位置变量
Private 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 Sub Form_Load()
Image1.Stretch = True
Timer1.Interval = 50
Timer1.Enabled = True
End Sub
Private Sub image1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Image1.Picture = LoadPicture("d:\3.jpg")
End Sub
Private Sub Timer1_Timer()
Dim MyRect As RECT
Dim MyPoint As POINTAPI
' MyRect返回当前窗口位置
GetWindowRect Me.hwnd, MyRect
' MyPoint返回当前鼠标位置
GetCursorPos MyPoint
If MyPoint.x < MyRect.Left Or MyPoint.x > MyRect.Right Or MyPoint.y < MyRect.Top Or MyPoint.y > MyRect.Bottom Then Image1.Picture = LoadPicture("d:\1.jpg")
End Sub
'用到一个API,声明如下:
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
'窗体上内放一个picturebox,添加代码:
Private Sub Form_Load()
SetCapture Picture1.hWnd
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X > 0 And X < Picture1.Width And Y > 0 And Y < Picture1.Height Then
Me.Caption = "in box"
'把这里换成你对图片的设定代码就行了
Else
Me.Caption = "out box"
'把这里换成你对图片的设定代码就行了
End If
End Sub
Public Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub picClose_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
If X > 0 And X < picClose.Width And Y > 0 And Y < picClose.Height Then
If Button = vbLeftButton Then
picClose.Picture = imgCloseDown.Picture
Else
picClose.Picture = imgCloseMove.Picture
End If
SetCapture picClose.hWnd
Else
picClose.Picture = imgClose.Picture
ReleaseCapture
End If
End Sub