I am developing a .NET windows app that needs to insert text in the place where the cursor is. The cursor will be in a different application that I have no control over. I think the operating system needs to be used here to achieve this. Can you help please?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Put the text you want to insert into the clipboard
Find the window's handle:
Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
// Do something with the handle...
}
Send a "Ctrl+V" message to the window using pinvoke, see http://www.pinvoke.net/default.aspx/user32.SendMessage
回答2:
The most easy is to use the SendKeys class on Windows.Forms.
Otherwise use the SendMessage from the windows api to send key by key (some information on this).
Do not use the clipboard, the user typically does not want that, and does not expect that.