C#模拟系统按键

摘自http://msdn.microsoft.com/zh-tw/library/ms171548(v=vs.110).aspx

首先,使用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函数获取到需要按键的窗口句柄,以计算器为例。这里体现了这个方法的局限性,就是似乎不能触发全局快捷键。

// (更多…)