C#获取窗口,模拟按键操作
2015-09-18 17:41
453 查看
C#获取窗口,模拟按键操作,实现计算器模拟操作。
![](http://images2015.cnblogs.com/blog/485336/201509/485336-20150918173721304-903952640.jpg)
首先引用。
使用DllImport引入两个函数:
然后首先使用FindWindow函数获取到需要按键的窗口句柄,以计算器为例。
![](http://images2015.cnblogs.com/blog/485336/201509/485336-20150918173721304-903952640.jpg)
首先引用。
using System.Runtime.InteropServices;
使用DllImport引入两个函数:
// Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd);
然后首先使用FindWindow函数获取到需要按键的窗口句柄,以计算器为例。
//FindWindow 参数一是进程名 参数2是 标题名 IntPtr calculatorHandle = FindWindow(null, "计算器"); //判断是否找到 if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("没有找到!"); return; } // 然后使用SetForegroundWindow函数将这个窗口调到最前。 SetForegroundWindow(calculatorHandle); //发送按键 SendKeys.SendWait("2"); SendKeys.SendWait("*"); SendKeys.SendWait("11"); SendKeys.SendWait("=");
相关文章推荐
- C#多线程编程
- zzIIS站点中部署WCF项目 - C#
- WPF--常见属性Label的隐藏和显示
- c# 调用存储过程
- C# 的 Task、Thread、ThreadPool 之间有什么异同?
- 重新学习C# 之 数据类型(二)
- C#图像遍历(续)
- c# 文件路径
- C#单链表的实现
- 【转载】想让你的R跟C#一样可以实…
- WPF--常见属性
- c#获取目录
- C#创建Windows Service(Windows 服务)基础教程
- C# Enum,Int,String的互相转换
- WPF--关于获取控件的属性
- c#写windows服务
- com和c#的一些笔记
- C# 格式化日期,货币
- C#删除只读文件
- c# 窗体插入WORD