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

方法

相關文章
相關標籤/搜索