我有一个窗口上面只放IMAGE控件,窗口的大小也IMAGE的大小相同.我想实现只要鼠标放在IMAGE图上IMAGE就换一张指定好的图(这已实现),当鼠标移开IMAGE时IMAGE将指定另一张图(这是在什么事件里写?)

解决方案 »

  1.   

    用MouseHook来解决,先取得当前窗体(Client区域)相对于屏幕的坐标(即:绝对坐标),然后取得鼠标移动时的绝对坐标,判断是否在该Client区域,一旦不在就更换另外一张图。
      

  2.   

    用有句柄的控件可以实现,比如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
      

  3.   

    建议换成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
      

  4.   

    因为窗口的大小与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