1. 程式人生 > >C# SetWindowPos視窗置頂使用說明

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