首页 > 代码库 > WinFrom窗体始终置顶

WinFrom窗体始终置顶

调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖:

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

使用方式:在需要置顶的窗体的Load方法里面加上

//Load  
private void Form1_Load(object sender, EventArgs e)  
{  
    try  
    {  
        SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2);  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show(ex.Message);  
    }  
    //3764             
}  

 

WinFrom窗体始终置顶