C#向一个窗口发送模拟按键的用法,自己的笔记贴

xjw163 2013-11-23 04:04:55

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string classname, string captionName);

//[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
//private extern static IntPtr FindWindowEx(IntPtr parent, IntPtr child, string classname, string captionName);

//[DllImport("user32.dll")]
//static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

static void Main(string[] args)
{

IntPtr mwh1 = IntPtr.Zero;

while (mwh1 == IntPtr.Zero)
{
Thread.Sleep(1000);
mwh1 = FindWindow(null, "无标题 - 记事本");
}


SetForegroundWindow(mwh1);
//发送字符串
System.Windows.Forms.SendKeys.SendWait("username");
//模拟tab键
System.Windows.Forms.SendKeys.SendWait("{TAB}");
//模拟ctrl+A全选
System.Windows.Forms.SendKeys.SendWait("^a");
//模拟回车键
//System.Windows.Forms.SendKeys.SendWait("{ENTER}");
}
}
}
...全文
383 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
全栈极简 2013-11-23
  • 打赏
  • 举报
回复
感谢楼主分享。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧