日期:2014-05-18 浏览次数:20796 次
using System; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace Class1 { /// <summary> /// WinAPI 的摘要说明。 /// </summary> public class SetWindow { [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern IntPtr GetForegroundWindow(); //WINAPI 获取当前活动窗体的句柄 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern bool SetForegroundWindow(IntPtr hWnd); //WINAPI 设置当前活动窗体的句柄 private Thread Th; private IntPtr MainInt; public SetWindow(IntPtr MainIntPrt) { MainInt=MainIntPrt; } /// <summary> /// 开始线程 /// </summary> public void Star() { Th=new Thread(new ThreadStart(SetForm)); Th.Start(); } private void SetForm() { while(true) { if(MainInt!=GetForegroundWindow())SetForegroundWindow(MainInt); Thread.Sleep(1); } } /// <summary> /// 关闭线程 /// </summary> public void Close() { Th.Abort(); } } }