Ok I have an app named Sharknadoo that I created,what this app does it reads the value from a combobox from 1 to whatever number and creates that number of textboxes in the right of it.
Now let us presume I do not have the code for this sharknadoo app just the app installed on my desktop.My question is how can I send my listbox.items from "My amazing app" to the sharknadoo textboxes? Presuming I have the same number of items in my listbox as I have textboxes in my other app.I am sorry but I really want to learn how to do this ,someone told me it is possible to achieve but I have no idea on how to achieve it was thinking about using coordinates or something like that,but from what I understood you can even hang on to the fact that the sharknadoo app is using textboxes without even having access to its source code.Thank you in advance friends :D.
Process[] processes = Process.GetProcessesByName("Sharknadoo.exe"); int i = 0; foreach (Process p in processes) { IntPtr windowHandle = p.MainWindowHandle; string item = listBox1.Items[i].ToString(); listBox1.Items.Add(item); i++; }
I realize the logic of my code is not good but it's all I could come up with.
1 Answers
Answers 1
This answer follows similar logic to your code, but instead simulates keyboard strokes and relies on using TAB to navigate boxes, but it should work in your case.
First add some code that we will use later to grab a link to your Sharknadoo application:
// Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd);
Now assuming you have not touched anything in the app (Very dangerous assumption, it would be better to launch Sharknadoo from your code before doing any of this), the tab index should be at 0 so we can do something like the following when you click the "Send to Sharknadoo" button:
// Send a your array of names to the Sharknadoo application. public void sendToSharknadoo(String[] detailsToSend) { // Get a handle to the Sharknadoo application. The window class // and window name can be obtained from Sharknadoo using the // Spy++ tool. IntPtr windowHandle = FindWindow("SharknadooFrame","Sharknadoo"); // Verify that Sharknadoo is a running process. if (windowHandle == IntPtr.Zero) { MessageBox.Show("Sharknadoo is not running."); return; } // Make Sharknadoo the foreground application and set the number // of text boxes for your info SetForegroundWindow(windowHandle); // Get to first box SendKeys.SendWait("{TAB}"); // enter number of boxes SendKeys.SendWait("{DOWN}"); SendKeys.SendWait((string)detailsToSend.Length); // Now enter your details into each of those boxes foreach (String s in detailsToSend) { // Get next textbox box SendKeys.SendWait("{TAB}"); // enter text into box SendKeys.SendWait(s); } }
With any luck that will do the trick. However you will probably need to mess with the order a bit put some checks in place.
Note: if you want a faster more aggressive approach that should execute before the user can interfere then try SendKeys.Send()
instead of SendKeys.SendWait()
Source:
https://msdn.microsoft.com/en-us/library/ms171548(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx
Additional Stack Overflow questions like this one:
0 comments:
Post a Comment