Mouse Hover

APD Toronto
APD Toronto used Ask the Experts™
on
Hi Experts

Using VBA in PowerPoint, how can I achieve a Mouse Hover effect, so when the user moves the mouse over a button it changes color, then when it moves it out it changes another color?  Is this the MouseDown event, using the X & Y values?

Also, colors such as &H00FFFFFF& - are these hexadecimal? If no, can they be or RGB?

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MSDN.... Action buttons are built-in button shapes (located in the Shapes gallery) that you can add to your presentation, and then assign an action to occur when you click ...

https://support.office.com/en-us/article/Assign-an-action-to-a-built-in-button-or-picture-4051F10F-5D82-4180-90E7-A91D54D86738

indicates you can add an action to MouseOver. But changing colors is not one of them , though maybe able to add a suitable VBA action if you can determine the calling shape.

see http://skp.mvps.org/ppt00040.htm#2 
        Sub Identify( oShp as Shape)

        Msgbox oShp.Name, vbInformation+vbOkOnly

        End Sub

Open in new window

Top Expert 2015
Commented:
What you describe is not MouseHover, it would be MouseEnter and MouseLeave.

MouseHover is when the mouse is floating over a control without moving, in contrast with MouseMove, which is triggered when the mouse is moving over a control. These 2 events are triggered repetitively at short interval while the condition is true.

Unfortunately, VBA in Office is very limited and does not offer MouseHover, MouseEnter and MouseLeave.

The only event that could be used for what you intend to do is MouseMove.

But there are 2 problems with that. First, it triggers continuously while the user moves the mouse over the control. So in some situations, the screen could flicker if there is too much code in the event. I do not think that it would be a problem with simply changing the color however.

The second problem is the worst. There is absolutely no way to know when to revert to the regular color. The controls offered with Office are not meant to create sophisticated user interfaces. They are simply too limited for your purpose.

Yes, &H00FFFFFF (you have an extra & at the end however) is hexadecimal. You can use it as is to define a color... as long as you know the value for the color that you seek.
APD TorontoSoftware Developer

Author

Commented:
robr,

Expanding on your code, I'm trying the folowing, but nothing is happening

Sub hoverCalculate(oShp As Shape)

    oShp.Fill.BackColor.RGB = RGB(255, 0, 0)

        
End Sub

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

APD TorontoSoftware Developer

Author

Commented:
Sorry, should've used ForeColor, which works.  However, what about reversing it back on MouseOut
APD TorontoSoftware Developer

Author

Commented:
@James, there will be only 4 lines of code if I use MouseMove, and there are Top and Left properties of the object.  Isn't MouseMove viable?
Top Expert 2015

Commented:
Yes, you could always try it, but it will be more than 4 lines of code.

The thing is that you cannot know from which side the user will be entering. Same thing for the exit.

And you cannot simply check for the values of 0, button width and button height to determine if the cursor is on a border. I cannot say for Office, because I never had to use MouseMove with Office, but in other environments, MouseMove trigger on a timer instead of on the pixels. Many pixels can be skipped between 2 MouseMoves, so the first MouseMove for a cursor entering the control top might be at 1, 2 or 4, maybe more. You would have to test to see the amount of pixels you should consider around the control.

It will also be hard, to determine when the user is getting out, many situations will have to be considered.

Say that the user enter from the Left on a MouseMove at 4, then suddenly moves back out of the control. You will get only the Enter and will not be able to detect the exit. You need at least 2 MouseMove in order to detect both entry and exit.

Then, think of a user that moves horizontally very close to the top. You will get a series of Top positions that could oscillate between, let's say, 2 and 4 because the user is just a bit shaky. How will you know, in the sequence 2233432342, that the last 2 is the one getting you out of the control?

In my opinion, doing it with the MouseMove can work most of the time if properly done, but there will be many situations in which you won't be able to detect the mouse getting out.

For a user, nothing is annoying as erratic behavior.

One idea that I have while writing this. Playing with events often asks for creative ways to fix things.

Behind your Button, put a Label that is larger than the Button and make it the same color as the background, so that it does not show. When the user gets out of the Button, the Label will start receiving MouseMove. This is your indication that the cursor is out and that the Button should get back to normal.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial