Thu, November 25, 2004, 02:46 AM under
MobileAndEmbedded
Assuming you read
ToolBarButton BUG and
ContextMenu.Show...
Let's look at the real solution. The title gives it away really: PInvoke
mouse_event. The API is pretty straightforward, but you must
RTFM and in particular the bit where it says "
...dx and dy contain normalized absolute coordinates between 0 and 65,535".
The DllImport is very easy (all parameters are Int32) and if you
STFNG you'll find
this. So here is a short sample for performing mouse clicks from code, using
mouse_event
. Create a winforms smart device project with a Form, 2 buttons and a MainMenu. The code should be self-explanatory and easily translatable to C#.
' Wrapper for mouse_event, performing click action on coordinates given
Public Shared Sub PerformMouseClick(ByVal aX As Int32, ByVal aY As Int32, ByVal aForm As Control)
Dim p As Point = aForm.PointToScreen(New Point(aX, aY))
Dim m1 As Int32 = (65535 \ Screen.PrimaryScreen.Bounds.Width)
Dim m2 As Int32 = (65535 \ Screen.PrimaryScreen.Bounds.Height)
Dim x As Int32 = m1 * p.X
Dim y As Int32 = m2 * p.Y
Win32Api.mouse_event(2 Or &H8000, x, y, 0, 0)
Win32Api.mouse_event(4 Or &H8000, x, y, 0, 0)
End Sub
' Button1 event handler. Simulates button click OR opening a main menu. Same principle applies for ToolBarButton
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PerformMouseClick(Button2.Left + 1, Button2.Top + 1, Me) 'performs click on other button
'PerformMouseClick(5, 5, Me) 'opens main menu on full screen WinCE app
'PerformMouseClick(Me.Left + 5, Me.Height + 5, Me) 'opens main menu on PPC app
End Sub
Finishing off, here are a few tips for playing with mouse coordinates and the above:
1. Use the static method
Control.MousePosition to determine where the mouse pointer is (or where you have sent it). E.g. run a timer and when it ticks update a listview with the coordinates.
2. Even better than 1, but only applicable if you are using Platform Builder, add the cursor to your image [it is under Shell_and_UI -> UI -> Mouse]. Now you can see where you are sending the mouse and where you exactly last tapped :-)
3. When passing the coordinates to mouse_event be sure to use the instance method
Control.PointToScreen as in the example given above (otherwise e.g. you'll be aiming for the toolbar and hitting the title bar)
Let me know if it (or anything else on this blog) doesn't work for you.