用有句柄的控件可以实现,比如PICTURE!!Public Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long Public Declare Function ReleaseCapture Lib "user32" () As LongPrivate 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
建议换成picturebox'用到一个API,声明如下: Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long'窗体上内放一个picturebox,添加代码: Private Sub Form_Load() SetCapture Picture1.hWnd End SubPrivate 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
因为窗口的大小与IMAGE的大小相同,可以用TIMER 控件来实现: '取得窗体位置的函数 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 POINTAPIx As Long y As LongEnd Type '窗体位置变量Private Type RECT Left As LongTop As Long Right As LongBottom 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 SubPrivate 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
Public Declare Function ReleaseCapture Lib "user32" () As LongPrivate 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
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long'窗体上内放一个picturebox,添加代码:
Private Sub Form_Load()
SetCapture Picture1.hWnd
End SubPrivate 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
'取得窗体位置的函数
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 POINTAPIx As Long
y As LongEnd Type
'窗体位置变量Private Type RECT
Left As LongTop As Long
Right As LongBottom 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 SubPrivate 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