您的位置:首页 > 编程语言 > C#

c#中借助API函数使窗口置顶

2009-01-16 17:00 267 查看
由于以前做一个小程序,想把窗口置顶,其实这个技术已经不是难题了,以前在VB中也是借助于API函数。但在C#中没想用API函数,至今没找到好办法,最后还得借助于API函数。
片断代如下:

[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);
这样,新打开的窗体就是置顶了。呵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: