就是有时候窗口不可以成功置顶,这时须要从新切换下标签,就能够置顶了,本文介绍C# SetWindowPos实现窗口置顶的方法:函数
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
/// <summary>
/// 获得当前活动的窗口
/// </summary>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern System.IntPtr GetForegroundWindow();this
哪一个窗体想要置顶,在Form_Load中加上 orm
SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2); //最后参数也有用1 | 4
具体说明,看API函数说明
若是是用点击一个按钮后弹出新窗体,并置顶,则:
Form2 frm = new Form2();
frm.Show();
SetWindowPos(GetForegroundWindow(), -1, 0, 0, 0, 0, 1 | 2);
方法