I want to press a button of an external window (a game table). To press buttons on external applications usually I use without problems SendMessage (everywhere is the cursor), but this specific window accepts SendMessage only if the mouse is over it.
Since I don't want the cursor moves over the window when the monitoring routine finds a button to press, I have made this code:
'X and Y are the coordinates of the point to press, into the button
Dim lParam As IntPtr = MakeLong(X, Y)
'block of the cursor (for few milliseconds)
Cursor.Clip = New Rectangle(Cursor.Position.X, Cursor.Position.Y, 1, 1)
'I move the mouse over the button
NativeMethods.SendMessage(TableHwnd, WM_MOUSEMOVE, IntPtr.Zero, lParam)
Sleep(30)
NativeMethods.SendMessage(TableHwnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam)
Sleep(40)
NativeMethods.SendMessage(TableHwnd, WM_LBUTTONUP, IntPtr.Zero, lParam)
'release of the cursor
Cursor.Clip = Rectangle.Empty
This code WORKS PERFECTLY if the blocked mouse cursor is OUT of the window: the button is pressed without the window gets focus. The cursor does'nt move. Perfect.
But if when I have to press the button the cursor is OVER the window the above code presses only under the cursor (i.e. pressing another button, terrible !!) ignoring lParam. Why? How to bypass the problem?
Since I don't want the cursor moves over the window when the monitoring routine finds a button to press, I have made this code:
'X and Y are the coordinates of the point to press, into the button
Dim lParam As IntPtr = MakeLong(X, Y)
'block of the cursor (for few milliseconds)
Cursor.Clip = New Rectangle(Cursor.Position.X, Cursor.Position.Y, 1, 1)
'I move the mouse over the button
NativeMethods.SendMessage(TableHwnd, WM_MOUSEMOVE, IntPtr.Zero, lParam)
Sleep(30)
NativeMethods.SendMessage(TableHwnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam)
Sleep(40)
NativeMethods.SendMessage(TableHwnd, WM_LBUTTONUP, IntPtr.Zero, lParam)
'release of the cursor
Cursor.Clip = Rectangle.Empty
This code WORKS PERFECTLY if the blocked mouse cursor is OUT of the window: the button is pressed without the window gets focus. The cursor does'nt move. Perfect.
But if when I have to press the button the cursor is OVER the window the above code presses only under the cursor (i.e. pressing another button, terrible !!) ignoring lParam. Why? How to bypass the problem?