代码未整理. namespace SendKeysTest { public partial class Form1 : Form { [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); public IntPtr HWND_TOP = IntPtr.Zero; public uint SWP_NOMOVE = 2; public uint SWP_NOSIZE = 1; public uint SWP_NOACTIVATE = 0x10; public uint SWP_SHOWWINDOW = 0x40; public Form1() {
namespace SendKeysTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public IntPtr HWND_TOP = IntPtr.Zero;
public uint SWP_NOMOVE = 2;
public uint SWP_NOSIZE = 1;
public uint SWP_NOACTIVATE = 0x10;
public uint SWP_SHOWWINDOW = 0x40;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Process[] vProcesses = Process.GetProcessesByName("notepad");
if (vProcesses.Length <= 0) return;
SetWindowPos(vProcesses[0].MainWindowHandle, HWND_TOP, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
this.timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
// string[] key = this.textBox1.Text.Split(',');
string[] key = new string[] { "1","space","2"};
SendKeys(key);
}
private void SendKeys(string [] key)
{
for (int i = 0; i < key.Length; i++)
{
System.Windows.Forms.SendKeys.Send(key[i]);
System.Threading.Thread.Sleep(2000);
} }
}
}
另外想问一下,
你的查找窗体是通过GetProcessesByName("notepad")
能不能通过我鼠标所在的窗体来实现?比如说,鼠标在写字板里面,这里就不要查找写字板的线程,而直接可以在写字板里面插入字符呢?
还有一个问题,如何把剪贴板中的数据 发送到word里面?用sendmessage不管用.
(不要用虚拟ctrl+v的方法)