服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - C# SetWindowPos窗口置顶使用说明

C# SetWindowPos窗口置顶使用说明

2019-10-12 11:13C#教程网 ASP.NET教程

就是有时候窗口不能够成功置顶,这时需要重新切换下标签,就可以置顶了,本文介绍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);


这样,新打开的窗体就是置顶了

延伸 · 阅读

精彩推荐