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