On Mouse Move

szaikin1
03-22-2003, 09:48 PM
I am trying to figure it out what code do I use when I move my mouse out of the command button to load different picture into the command button

example:

1st I make a command button graphical then
I load a picture into the command button in design time, then
when I move mouse over different picture is loaded, and when I move my mouse out of the command button different picture has to be loaded, That where I am having trouble.

Some one please help.

Thanks

Deadalus
03-22-2003, 10:34 PM
The problem is that controls don't have a "mouse leave" event. You can use the API functions SetCapture and ReleaseCapture to get around this. A small example will probably be simpler than explaining this. Paste the code below in the form of a new project and put one command button on the form (leave the default name Command1). If it isn't clear how this works, just ask.

Option Explicit

Dim blnOnButton As Boolean

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

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X < 0 Or X > Command1.Width Or Y < 0 Or Y > Command1.Height Then 'mouse is not over button
Command1.Caption = "Mouse out"
ReleaseCapture 'release mouse capture
blnOnButton = False
ElseIf Not blnOnButton Then
blnOnButton = True
SetCapture (Command1.hwnd) 'send all mouse events to our button
Command1.Caption = "Mouse over"
End If
End Sub

Private Sub Form_Load()
Command1.Caption = "Mouse out"
End Sub
Corrected declaration.

phlatline
03-22-2003, 10:56 PM
I had this same problem but I took the not so creative way out. I simply put a Sub Form_MouseMove function in that changed the button image. Because my button rested on the form whenever your mouse left the button it hit the form. It's not the best way to do it, but it's really simple.

Deadalus
03-22-2003, 11:10 PM
Yes, that's the quick and dirty way. But the Form_mousemove event will not always fire if other controls are close to your button or you move the mouse too fast to another control or outside the form.

phlatline
03-23-2003, 02:40 AM
Well I said it wasn't the best way.

szaikin1
03-23-2003, 03:39 PM
I have used the code referenced above, and came up with another problem.

The problem is that when you click on command button it executes the .setfocus to another control or brings up the msgbox. When it does either of those the picture then does not change anywhere and the one that was there when mouse moved over it stays there permanent.

P.S. I hope this can be fixed somehow. I've tried to put code in command1_lostfocus but that does not change anything.

Squirm
03-23-2003, 04:16 PM
You need to add extra code to the Click event of the button then.

Deadalus
03-23-2003, 04:17 PM
Yes, that can be a problem. The easiest solution is to treat such a change of focus as a mouse leave. For example:
Private Sub Command1_Click()
Command1.Caption = "Mouse out"
ReleaseCapture
blnOnButton = False
MsgBox "Test"
End Sub
That does have the effect that when the messagebox shows, the text on the button (or the picture) will be the one for being outside, even while the mouse cursor stays over the button. But it's less inconvenient than the mouse over state showing. And when the message box disappears or the focus comes back it should work normal again.

There's probably a more elegant solution for this, but I don't have time to check right now. If you want, you can take a look at the code for the HoverCommand button at EliteVB.com, that uses this method too.

A totally different method is using a quick timer that keeps checking if the mouse is over the button. But to do that well might not be trivial either (and I haven't really looked into it myself).

szaikin1
03-23-2003, 06:00 PM
Yes, that can be a problem. The easiest solution is to treat such a change of focus as a mouse leave. For example:
Private Sub Command1_Click()
Command1.Caption = "Mouse out"
ReleaseCapture
blnOnButton = False
MsgBox "Test"
End Sub
That does have the effect that when the messagebox shows, the text on the button (or the picture) will be the one for being outside, even while the mouse cursor stays over the button. But it's less inconvenient than the mouse over state showing. And when the message box disappears or the focus comes back it should work normal again.

There's probably a more elegant solution for this, but I don't have time to check right now. If you want, you can take a look at the code for the HoverCommand button at EliteVB.com, that uses this method too.

A totally different method is using a quick timer that keeps checking if the mouse is over the button. But to do that well might not be trivial either (and I haven't really looked into it myself).
Thank you very much, I will take a look at HoverCommand Button.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum