先用和FindWindow 这个WindowsAPI函数找到你要打字的窗体,让后用SetForegroundWindow激活这个窗体,让后调用SendKeys.send 直接发送字符串就行了,给你举个例子吧:
直接上代码:
(先在桌面新建一个文本文件 ,命名为:123.txt , 然后双击打开123.txt 然后运行本程序,单击 button1)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "FindWindow", SetLastError = false,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32", EntryPoint = "SetForegroundWindow", SetLastError = false,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int SetForegroundWindow(
IntPtr hWnd
);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwnd = (IntPtr)FindWindow(null, "123.txt - 记事本");
SetForegroundWindow(hwnd);
SendKeys.Send( "先用和FindWindow 这个WindowsAPI函数找到你要打字的窗体,让后用SetForegroundWindow激活这个窗体,"+
"让后调用SendKeys.send 直接发送字符串就行了,给你举个例子吧:\n\r"+
"先在桌面新建一个文本文件 ,命名为:123.txt , 然后双击打开123.txt 然后运行本程序,单击 button1 "+
"效果不错吧,是你想要的吧,O(∩_∩)O哈!"
);
}
}
}