using System.Runtime.InteropServices;//与API相关
using System.Diagnostics;//与进程相关
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();//获得即获得前台窗口句柄
函数(){
IntPtr ii = GetForegroundWindow();//获得即获得前台窗口句柄
Process p = new Process();//实例一个进程
p = Process.GetProcessesByName("msnmsgr")[0];//以msn为例,msnmsgr为进程名称
IntPtr jk = p.MainWindowHandle;
if (ii == jk)
{
//加入你想要的动作
}
}