Send Click Message to another application process

2020-04-23 06:41发布

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.

标签: c# .net winapi
2条回答
看我几分像从前
2楼-- · 2020-04-23 07:14

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.

查看更多
放荡不羁爱自由
3楼-- · 2020-04-23 07:21

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

查看更多
登录 后发表回答