将信息从我的列表框发送到不同的应用程序文本框

Sending information from my listbox to different app textboxes

好的,我创建了一个名为 Sharknadoo 的应用程序,该应用程序的作用是从组合框中读取从 1 到任意数字的值,并在其右侧创建该数量的文本框。

现在让我们假设我没有这个 sharknadoo 应用程序的代码,只是安装在我的 desktop.My 上的应用程序问题是如何将我的 listbox.items 从 "My amazing app" 发送到sharknadoo 文本框?假设我的列表框中的项目数量与我的其他列表框中的文本框数量相同 app.I 很抱歉,但我真的很想学习如何做到这一点,有人告诉我这是可能实现的,但我不知道如何实现它正在考虑使用坐标或类似的东西,但据我了解,你甚至可以坚持这样一个事实,即 sharknadoo 应用程序正在使用文本框,而无需提前访问其源代码 code.Thank 朋友们: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++;         
            }

我意识到我的代码逻辑不好,但我只能想出这些了。

此答案遵循与您的代码类似的逻辑,但它模拟键盘敲击并依赖于使用 TAB 来导航框,但它应该适用于您的情况。

首先添加一些代码,稍后我们将使用这些代码来获取 link 到您的 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,以便我们可以执行类似的操作单击 "Send to 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()


来源:

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 问题:

Insert text into the textbox of another application