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

哪個窗體想要置頂,在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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM