Articles

VB6 MouseMove

February 21, 2005 at 22:07:58
Specs: XP Pro SP1, Athlon XP 2600+ / 1024Mb

I'm trying to make a simple screensaver in VB6. I have everything working properly except the movement of the mouse. It seems that no matter what code I use in the MouseMove event to end the program, when I run it, it ends almost immediately. I thought it might be a problem with the machine I'm using, so I compiled it and moved it to two other machines, but the same thing happens. One thing to note is that all 3 machines are using optical mice ( or is that mouses? or meeces?). Other screensavers don't seem to have this problem on these machines, so I assume that it has nothing to do with the optical mice. Can anyone shed some light on this problem?


Please let us know if you found someone's advice to be helpful.


See More: VB6 MouseMove

Report •


#1
February 21, 2005 at 22:24:31

They address that issue and give a few other tips here.

Report •

#2
February 21, 2005 at 23:17:15

That's the same code that Microsoft suggest, and it produces the same results. Any other suggestions?


Please let us know if you found someone's advice to be helpful.


Report •

#3
February 21, 2005 at 23:29:23

Just a thought... try putting a label on your form and replace your mousemove code with the code below. That will at least tell you if the event is being constantly triggered or if you just need to increase the value to test it against before calling the End.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)

Static intMouseCount As Integer

Label1.Caption = intMouseCount
intMouseCount = intMouseCount + 1


End Sub


Report •

Related Solutions

#4
February 21, 2005 at 23:32:08

You could also try putting a timer on the form (of maybe one second), and not allowing the mousemove even to end the program until the timer has fired.

Report •

#5
February 21, 2005 at 23:33:00

>mousemove even to end
mousemove event to end

Report •

#6
February 21, 2005 at 23:47:50

I tried your suggestion with the label and discovered that the event is, in fact, being constantly triggered. How can I fix this?

I don't understand your suggestion with the timer. How would I use the timer to control the mousemove event?


Please let us know if you found someone's advice to be helpful.


Report •

#7
February 22, 2005 at 00:15:00

The timer would just be used to trip a flag variable that the mousemove tests for, but would only help if there were only a few initial mousemove events. If the mousemove event doesn't stop getting tripped, that idea won't help.

If you just sit and watch, the counter just keeps going up and up on its own... never stops? That's odd because if the mouse keeps generating movements, it would be affecting your other screensavers as well... and the event should only fire when the mouse actually moves.

The only other thing I can suggest right now is to make a new, 'bare bones' project with just enough code to see if the same deal happens with the mousemove event.

I'm off for the night, but if you're still having trouble you can zip up the project (or enough of it to show it's not working)and email it to me. If I get the same effect on this end, I'll see if I can find the cause.


Report •

#8
February 22, 2005 at 10:21:32

Not to be a jerk, but those suggestions are really bad. Not only do they add to the complexity of the problem, they are not sound programming practices.

Anyone here who knows me, knows that I am a VB advocate (except when the job is clearly too complicated for VB...ie pointers etc)

So I like to educate VB programmers (i don't mean this in a bad way) how to be better software engineers and not just vb hacks.

So...to that end, here is the proper way to handle this issue:

-------
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type

Dim DeltaX As Integer
Dim DeltaY As Integer
Dim StartPos As POINTAPI

Private Sub Form_Load()
' these should be set when the screen saver is in setup mode
DeltaX = 120 ' adjust this for x sensitivity level
DeltaY = 120 ' adjust this for y sensitivity level
Call GetCursorPos(StartPos)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim NewPos As POINTAPI
Call GetCursorPos(NewPos)
Dim CheckX As Integer
Dim CheckY As Integer
CheckX = Abs(NewPos.X - StartPos.X)
CheckY = Abs(NewPos.Y - StartPos.Y)
If CheckX > DeltaX Or CheckY > DeltaY Then
Unload Me
End If

End Sub
-------

Here is the explanation of this code:
1) GetCursorPos is a Win32 API function that returns a point (x and y coord) of the current mouse position on the screen (regarless of which form has focus)
2) when the form loads (in screensaver mode) you capture the cursor position and load (or in this example's case set) the sensitivity by using to delta variables. (in case you don't know, delta is a mathematical term meaning "change in")
3) then on mousemove event, you capture the mouse's position again using the getcursorpos win32 api function (so that you are not using vb's screwy coord system) and compare it to your deltas. If they exceed the sensitivity, unload the screensaver.

now, there are several things that you should take care of in your screensaver app.
1) You should display a tiny version of it in the screensaver's preview pane.
2) you should allow the user to set some values (such as the sensitivity) by responding to the setup request from the screensaver preview screen
3) you should make certain that the program only loads one time EVER, meaning that it only allows for one instance of itself
4) you should allow it to work with the password protection system (win9x/me as well as the 2k/xp style)

I have a very nice screensaver system written in C++ that you are welcome to, VB can do all this too (there is a handle issue with #1 above, but it is pretty easy to overcome using Win32 API calls)

If you need anymore help, please contact me maslow_malo@hotmail.com

Chi

PS. Sorry if I made you mad hijinx...but i gotta educate the masses.

They mostly come at night...mostly


Report •

#9
February 22, 2005 at 12:50:13

Sorry if I made you mad hijinx

No worries, you didn't. Temporarily putting the label on may have 'added complexity' but it did tell him that the problem was with the mousemove event firing when it shouldn't have.

Using stripped down code is often used to narrow down/confirm where problems lie. Even though your code should fix him, I'd still be wondering why that code was being executed on 3 different machines when the mouse was still. Chalk it up to 3 nervous rodents maybe.

Yes, the timer was a kludge but I dropped that anyway.

but i gotta educate the masses

that's cute


Report •

#10
February 22, 2005 at 17:53:58

HiJinx,

I had another look at your idea with the label and found that the counter would sometimes pause for a short time. By not hiding the mouse, I discovered that the pause coincided with the cursor being over one of the moving controls I had on the form. If I removed all the controls that I had moving around, the mousemove event was no longer being continually triggered.

Chi,

Your code fixed a part of the problem - getting the mousemove event to work as it should. Your explanation of the code fixed the rest of my problem - being able to understand what the code does.

Thanks heaps for the help, guys.


Please let us know if you found someone's advice to be helpful.


Report •


Ask Question