萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> C#SetWindowPos窗口置頂

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();

哪個窗體想要置頂,在Form_Load中加上

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);

copyright © 萬盛學電腦網 all rights reserved