2010-12-23 107 views
1

我有一个场景,我需要将点击事件发送到一个独立的应用程序。我用下面的代码启动了该应用程序。发送点击消息到另一个应用程序进程

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

现在我想发送鼠标单击消息到该应用程序,我有相对于应用程序窗口的具体坐标。我如何使用Windows Messaging或任何其他技术来做到这一点。

我用

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

它运作良好,但会使指针移动为好。所以不适合我的需要。

然后我用。

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

它适用于最小化最大化,但不适用于鼠标事件。

我使用mousevents的代码是,

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 

感谢提前的帮助,并等待反馈。

+2

停止发送输入消息,他们应该发布。无论如何,使用`SendInput()`可能会更好,这是虚假输入的正确方法。 – 2011-03-04 11:18:29

回答

0

保存光标位置,使用mouse_event并将光标移回。 mouse_event确实是做到这一点的最佳方式。

+0

但是为什么SendMessage不工作? – 2010-12-23 12:27:55

+0

单击就会涉及多个消息,如鼠标移动,按钮向下,按钮向上,单击以及其他可能的消息。我并不是说用SendMessage来模拟是不可能的,因为它肯定是,它更难。您应该创建一个简单的表单应用程序,将所有收到的消息转储到文本文件并查看会发生什么。 – fejesjoco 2010-12-23 12:34:52

2

对于单击,您应该同时发送两个鼠标事件以进行精确的鼠标单击事件。

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

它在我的项目中工作。

相关问题