C# 窗体永远在最前
1、调用系统API
public const int HWND_TOP = 0;public const int HWND_BOTTOM = 1;public const int HWND_TOPMOST = -1;public const int HWND_NOTOPMOST = -2;//设置此窗体为活动窗体://将创建指定窗口的线程带到前台并激活该窗口。键盘输入直接指向窗口,并为用户更改各种视觉提示。//系统为创建前台窗口的线程分配的优先级略高于其他线程。[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体://激活窗口。窗口必须附加到调用线程的消息队列。[DllImport("user32.dll", EntryPoint = "SetActiveWindow")]public static extern IntPtr SetActiveWindow(IntPtr hWnd);//设置窗体位置[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);
2、函数调用(放在构造或Load)
// 设置窗体显示在最上层SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010 | 0x0080);// 设置本窗体为活动窗体SetActiveWindow(this.Handle);SetForegroundWindow(this.Handle);// 设置窗体置顶this.TopMost = true;
文章抄录自 c#让窗体永在最前 调用windows api 将窗体设为topmost