C# code
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();
}
}
}
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();
}
}
}
调用:
SetWin =new Class1.SetWindow(this.Handle);
SetWin.Star();