Send mouse click to only specified window

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 1
Reputation: PRWang is an unknown quantity at this point 
Solved Threads: 0
PRWang PRWang is offline Offline
Newbie Poster

Send mouse click to only specified window

 
1
  #1
Oct 12th, 2008
I have a vb.net 2008 application that is supposed to send a mouse click to a specified window. This can be any window, and the user selects the correct one from a list box during configuration. The user can also change the window or the mouse button to click at any time.

Everything works fine, window selection, click interval, right or left button, etc. except for one thing. If I move the cursor out of the window, it will click at least one time on whatever window is under the cursor when the event fires, including the desktop.

I have an if statement that checks to see if the window under the cursor is the selected window before firing the mouse event, and it seems to work except for the first click after I leave the window. I call the mouse event based on the timer interval and then check for the correct window within the mouse click subroutine.

Here is the code that calls the timer, the timer, and the mouse click code. Why is it clicking outside the window?
  1. '
  2. ' Start the timer and start clicking
  3. '
  4. ' numUpDown.Value is set by the user with a NumericUpDown control
  5. Timer1.Interval = numUpDown.Value * 1000 ' Timer runs in milliseconds
  6. Timer1.Enabled = True
  7.  
  8. Private Sub Timer1_Tick( _
  9. ByVal sender As System.Object, _
  10. ByVal e As System.EventArgs) _
  11. Handles Timer1.Tick
  12. '
  13. ' Do not re-initialize blink timer if it
  14. ' is already running This timer is used to let the user
  15. ' know the click timer is running.
  16. '
  17. If Timer2.Enabled = False Then
  18. Timer2.Interval = 1000 ' One second timer to blink
  19. Timer2.Enabled = True ' red and green
  20. End If
  21. ' strMouseAction is set by user to be "R" or "L" for mouse button
  22. Call subMouseClick(strMouseAction)
  23. Application.DoEvents() ' Allow for exit
  24. End Sub
  1. Public Sub subMouseClick(ByVal Action As String)
  2. Dim ptrFGWindow As IntPtr = GetForegroundWindow()
  3. Dim dwFlagsD As Int32 ' mouse button down
  4. Dim dwFlagsU As Int32 ' mouse button up
  5. '
  6. ' Check to make sure the proper window is top window
  7. ' Do not send mouse clicks to undesired windows
  8. '
  9. ' i64Handle is actually hWnd
  10. If i64Handle = ptrFGWindow Then
  11. Select Case Action
  12. Case "L"
  13. dwFlagsD = MOUSEEVENTF_LEFTDOWN
  14. dwFlagsU = MOUSEEVENTF_LEFTUP
  15. Case "R"
  16. dwFlagsD = MOUSEEVENTF_RIGHTDOWN
  17. dwFlagsU = MOUSEEVENTF_RIGHTUP
  18. End Select
  19. mouse_event(dwFlagsD, 0&, 0&, 0, 0)
  20. mouse_event(dwFlagsU, 0&, 0&, 0, 0)
  21. End If
  22. End Sub
If my code looks strange, I'm an old retired programmer from the days when CBASIC was the greatest thing to ever happen to BASIC. All this "Visual" stuff is anything but visual to me, but I'm learning.

Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: TheKangaroo is an unknown quantity at this point 
Solved Threads: 0
TheKangaroo TheKangaroo is offline Offline
Newbie Poster
 
0
  #2
Oct 13th, 2009
I am sorry, but it seems I dont fully understand your question:

You get the handle from the foregroundwindow (=ptrFGWindow) and compare it with the i64 handle, I assume this is the hwnd of you application window ?

Nevertheless your application can be in the foreground without the mouse beeing in the client area, so what is this going to prove ?

If you want to be absolutely sure the mouse pointer is inside the client area, you would have to call GetWindowInfo, check the windowsposition and compare it with the mouse position on the screen.

But as I said, I am not quite sure I understand the problem in full ...
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC