好吧,我创建了一个名为Sharknadoo的应用程序,该应用程序执行的操作是将组合框中的值从1读取到任意数字,并在其右侧创建该数量的文本框。现在让我们……
这个答案遵循与您的代码类似的逻辑,但相反模拟键盘笔划并依赖于使用TAB导航框,但它应该适用于您的情况。
首先添加一些我们稍后将用于获取Sharknadoo应用程序链接的代码:
// 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);
现在假设您没有触及应用程序中的任何内容(非常危险的假设,最好在执行任何此操作之前从代码中启动Sharknadoo),选项卡索引应该为0,因此当您单击时我们可以执行以下操作“发送到Sharknadoo”按钮:
// 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); } }
任何运气都可以解决问题。但是你可能需要把订单弄得乱七八糟。
注意:如果您想要一个更快更积极的方法,应该在用户干扰之前执行,然后尝试 SendKeys.Send() 代替 SendKeys.SendWait()
SendKeys.Send()
SendKeys.SendWait()
资源:
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
其他Stack Overflow问题如下:
将文本插入另一个应用程序的文本框中