[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow(); //获得本窗体的句柄
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
public IntPtr Handle1;
Load方法中; Handle1 = this.Handle;
timer1.Enabled = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (Handle1 != GetForegroundWindow()) //持续使该窗体置为最前,屏蔽该行则单次置顶
{
SetForegroundWindow(Handle1);
//timer2.Stop();//此处可以关掉定时器,则实现单次置顶
}
}