Hello. I have been searching for some time now to find a way to send mouse clicks to another application not using mouse x,y coordinates, and I have not yet found a solution (either that posts/examples are not even compilable w/o info to proceed, or the info is not there to get me started). I would like to send mouse clicks to another application (that is not written .net). I am not all that C# guru, my background is embedded systems.
I am starting the application that I want to send mouse clicks to, via
startInfo = new ProcessStartInfo(@"C:\Code\abc.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
hence, the application receiving the mouse clicks does not have focus and is running as hidden. Some posts mentioned to use WinID program to get info about the buttons for which to send mouse clicks.
Can anyone provide an end-to-end solution for this problem? I have tried many different ways for the past few days now and have not reached a conclusive method. Your help or direction is greatly appreciated.
Is my current way even possible? i.e running application in hidden style and sending mouse events.
(btw. I am using Visual Express2008 with .NET 3.5 SP1. )
thanks
Loading

Danatas GerviRank #4,152 · 361 reputationPosted Oct 1, 2009, 5:58 AM EDT
public partial class B : Form { const int WM_COMMAND = 0x111; const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; const int VK_X = 0x58; [DllImport("user32.dll")] public static extern void SetWindowText(int h, String s); [DllImport("user32.dll")] private static extern int FindWindow(string ClassName, string WindowName); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] private static extern bool PostMessage(IntPtr hwnd, int Msg, long wParam, long lParam); public B() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) {int hWnd = FindWindow(null, "1.txt - Notepad"); if (hWnd > 0) { IntPtr intptr = new IntPtr(hWnd); IntPtr intptrNULL = new IntPtr(); IntPtr hWndC = FindWindowEx(intptr, intptrNULL, "Edit", ""); bool result = PostMessage(hWndC, WM_CHAR, VK_X, 0); } ?Laith11 reputationPosted Oct 1, 2009, 10:43 AM EDT
I had to add another dll import for it to compile...
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
Still the Edit button on the note pad is not firing. Not sure what is the problem. Despite I had the notepad maximized.
Also, what is VK_X represents?
thanks
Laith11 reputationPosted Oct 1, 2009, 11:08 AM EDT
const int WM_LBUTTONDOWN = 0x0201;
bool result = PostMessage(hWndC, WM_LBUTTONDOWN, ???, 0);
thanks
Roei BarRank #1,271 · 1.4K reputationPosted Oct 1, 2009, 11:10 AM EDT
http://xdmessaging.codeplex.com/
its a way to send messages via windows messaging
Laith11 reputationPosted Oct 1, 2009, 4:38 PM EDT
Master BillaRank #2,229 · 630 reputationPosted Oct 1, 2009, 7:45 PM EDT
You can do with this through the message passing between exes.
http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c7259
thank you
Laith11 reputationPosted Oct 1, 2009, 8:31 PM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Oct 1, 2009, 11:08 PM EDT
here is the article for how to use mouse events
http://www.pinvoke.net/default.aspx/user32/mouse_event.html
Laith11 reputationPosted Oct 2, 2009, 5:18 PM EDT