How to implement a "MouseOver" function?

Posted on 2003-03-10
Medium Priority
Last Modified: 2010-05-01
I need to know when the mouse move from inside a picbox (whn i use the mousemove event) to outside.
The porblems are that
1) no buttons are pressed, so the picbox don't "capture" mouse moves outside itself
2) I've too many controls around that picbox to write a mousemove event in each to know that the mouse in now outside the picbox, and the form space between all controls are to little to be sure that form detect a mousemove.

So i think i need some API stuffs.. please write some pseudo working code... thanks
Question by:fcp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

ID: 8107680
You will have to write the code in MouseMove event of the form also. This is becuase as soon as the mouse moves outside picturebox the mouseevent of the form gets fired. In case there is some other control attached with the picturebox and the form is not visible then write code in Mousemove event of that control.
LVL 18

Expert Comment

ID: 8107689
To shorten the coding effort declare a sub and write the effective code in that sub. Then simply call that sub in MouseMove event of all the controls.
LVL 12

Accepted Solution

jgv earned 1000 total points
ID: 8108157
Start a new project, draw a picturebox on the form and add this code:

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long

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
        If GetCapture <> Picture1.hwnd Then
            Picture1.BackColor = vbRed
            SetCapture Picture1.hwnd
        End If
        Picture1.BackColor = Me.BackColor
    End If
End Sub
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8109030
Nice code, jgv.  Too bad it doesn't work with Images though.

Expert Comment

ID: 8109355

Here is an article (and source code) that may help you:

"Generating MouseLeave Events for a Window"


Expert Comment

ID: 8109521
Hello fcp,

try doing this.
1.Take a global varibale as boolean.
2.Set default value to false.
3.Whenever mousemoves on the PictureBox, make the variable value to True.
Write the condition as:
if <GlobalVariable> = false then
<GlobalVariable> = true
end if
4.Thus you will get the exact point when the mouse moves over the PictureBox.
5.In the Form_Maousemove, set the variable to false only if it is already true.
Write the condition as:
if <GlobalVariable> = true then
<GlobalVariable> = false
end if
6.Do the same thing in Other Controls' MouseMove.
7.Choose only those controls which are very close to the PictureBox, so that you will not have to write the code,in each and every control's MouseMove event.
8.Doing this, your logic with the other program will remain inract.
9. But I think capturing,both these events should solve your problem.
10.As we have taken a variable, on basic of this you can also code, if required.

All The Best.

Author Comment

ID: 8110510
WOW! Very fast. THANKS

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question