本文介绍了我如何发送键presses到正在运行的进程对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让C#启动应用程序(在这种情况下,开放式办公),并开始发送该应用程序键presses这样,它会出现,就像有人在打字。所以,理想情况下,我将能够发送运行的开放式办公过程中的关键preSS字母D,与开放式办公会,然后输入d在纸张上。谁能给我方向,每如何去了解呢?我试图做到以下几点:

I am trying to make C# launch an application (in this case open office), and start sending that application keypresses such that it would appear as though someone is typing. So ideally, I would be able to send a running open office process the keypress for the letter "d", and open office would then type d on the paper. Can anyone give me direction as per how to go about this? I have tried to do the following:

p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = processNames.executableName;

p.Start();

p.StandardInput.Write("hello");

但是,这并不让我想要的效果 - 我没有看到文字输入在开放式办公

But that doesn't get me the desired effect - I don't see the text typed out in open office.

推荐答案

您必须通过Win32的sendmessages做到这一点:其基本思路是这样的:

You have to do this via Win32 sendmessages: The basic idea is like this:

拳,你需要一个指向启动的过程窗口:

Fist you need a pointer to the launched process window:

using System.Runtime.InteropServices;

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

private void button1_Click(object sender, EventArgs e)
{
  // Find a window with the name "Test Application"
  IntPtr hwnd = FindWindow(null, "Test Application");
}

然后用SendMessage函数或PostMessage的(preferred你的情况我猜):

then use SendMessage or PostMessage (preferred in your case I guess):

http://msdn.microsoft。 COM / EN-US /库/ ms644944(V = VS.85)的.aspx

在此消息指定了正确的消息类型(例如WM_KEYDOWN)发送键preSS:

In this message specify the correct message type (e.g. WM_KEYDOWN) to send a keypress:

http://msdn.microsoft.com/ EN-US /库/ ms646280(VS.85)的.aspx

看一看 PInvoke.net 得到的PInvoke源$ C ​​$ C。

Have a look at PInvoke.net to get PInvoke sourcecode.

另外,您可以使用FindWindow函数来使该窗口前台后使用SendKeys.Send(.NET)方法。不过,有点不可靠的。

Alternatively you can use the SendKeys.Send (.Net) method after using FindWindow to bring that window to the foreground. However that is somewhat unreliable.

这篇关于我如何发送键presses到正在运行的进程对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:11