前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WinForm SetWindowPos窗口置顶使用说明

WinForm SetWindowPos窗口置顶使用说明

作者头像
跟着阿笨一起玩NET
发布2019-06-14 11:30:30
2.3K0
发布2019-06-14 11:30:30
举报

就是有时候窗口不能够成功置顶,这时需要重新切换下标签,就可以置顶了,本文介绍C# SetWindowPos实现窗口置顶的方法:

代码语言:javascript
复制
[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中加上

代码语言:javascript
复制
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);

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档