Nazar Hussain
Nazar Hussain

Reputation: 5162

Send Click Message to another application process

I have a scenario, i need to send click events to an independent application. I started that application with the following code.

private Process app;
app = new Process();
app.StartInfo.FileName = app_path;
app.StartInfo.WorkingDirectory = dir_path;
app.Start();

Now i want to send Mouse click message to that applicaiton, I have specific coordinates in relative to application window. How can i do it using Windows Messaging or any other technique.

I used

[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);

It works well but cause the pointer to move as well. So not fit for my need.

Then i use.

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

It works well for minimize maximize, but do not work for mouse events.

The codes for mousevents i am using are,

WM_LBUTTONDOWN = 0x201, //Left mousebutton down
WM_LBUTTONUP = 0x202,   //Left mousebutton up
WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
WM_RBUTTONDOWN = 0x204, //Right mousebutton down
WM_RBUTTONUP = 0x205,   //Right mousebutton up
WM_RBUTTONDBLCLK = 0x206, //Right mousebutton do

Thanks for the help in advance, and waiting for feedback.

Upvotes: 1

Views: 11259

Answers (2)

IsozWorld
IsozWorld

Reputation: 59

For a single click you should send two mouse event at the same time for an exact mouse click event.

SendMessage(nChildHandle, 0x201, 0, 0); //Mouse left down
SendMessage(nChildHandle, 0x202, 0, 0); //Mouse left up

It is working in my project.

Upvotes: 2

fejesjoco
fejesjoco

Reputation: 11903

Save the cursor position, use mouse_event and move the cursor back. mouse_event is really the best way to do this.

Upvotes: 0

Related Questions