|
-
Jul 9th, 2009, 05:47 PM
#1
Thread Starter
New Member
Trouble Using Mouse Events to Click on Form
I have set up a user for to contain a WebBrowser object which is a "wrapper" for a flash based, online music player. I would like to add a system tray icon/menu to play/pause, etc, but because I cannot interface directly with the flash object, I have to trigger a mouse event at a know screen location; I can predict where the play button is because I know where the form is as well as the location of the button within the form. Doing only that is not a problem.
However, in order to keep everything pretty, I would like to keep the player off screen to free up screen real estate. From what I can tell, the cursor can NOT be moved off-screen; therefore, the form then must move ON-screen, the cursor must be moved to where the play button is, the mouse-click event must be triggered, then both the form and the cursor must be put back.
And THAT is what is giving me the trouble. For some reason, regardless of the order of my code, the mouse click does not actually fire until AFTER the form has been moved back. I have posted the relevant code snippets below. Feel free to steal any of it (that you can get to work). 
What I need to solve:
Fundamentally, I need to be able to simulate a click on a WebBrowser object that is off screen. What is currently not working is that even though the form is moved back AFTER the mouse click events are triggered, the click doesn't "happen" until a split second after the form is moved back. How can I solve this, or is there a smarter way to accomplish what I need?
(BTW, I have tried refreshing the form, "sleeping" the thread, etc., and the mouse still waits 'til after ALL THAT to finally click.)
The Code:
Code:
Private Sub PlayToolStripMenuItem_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles PlayToolStripMenuItem.Click
Dim TempPt As New System.Drawing.Point
Dim Mouse As New MouseTools
' saves original cursor and form locations
Dim OriginalCursorLocation As System.Drawing.Point = Windows.Forms.Cursor.Position
Dim OriginalFormLocation As System.Drawing.Point = Me.Location
' moves form to top left of screen
TempPt.X = 0
TempPt.Y = 0
Me.Location = TempPt
' moves cursor over the play buton on the form
TempPt.X = 64
TempPt.Y = 138
Windows.Forms.Cursor.Position = TempPt
' raises the left mouse button down/up events
Mouse.Trigger(MouseTools.MouseEvents.ButtonLeftDown)
Mouse.Trigger(MouseTools.MouseEvents.ButtonLeftUp)
' restores original cursor and form locations
Windows.Forms.Cursor.Position = OriginalCursorLocation
Me.Location = OriginalFormLocation
End Sub
The following class allows access to the mouse event stuff:
Code:
Class MouseTools
' Provides almost brainless accessability to the windows library function
Private Declare Sub lib_mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As MouseTools.MouseEvents, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Sub Trigger(ByVal Flags As MouseEvents)
lib_mouse_event(Flags, 0, 0, 0, 1)
End Sub
Public Enum MouseEvents As Integer
MoveAbsolute = &H8000
ButtonLeftDown = &H2
ButtonLeftUp = &H4
Move = &H1
ButtonMiddleDown = &H20
ButtonMiddleUp = &H40
ButtonRightDown = &H8
ButtonRightUp = &H10
End Enum
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|