C# 窗体永远在最前

C# 窗体永远在最前

1、调用系统API

csharp 复制代码
 public const int HWND_TOP = 0;
 public const int HWND_BOTTOM = 1;
 public const int HWND_TOPMOST = -1;
 public const int HWND_NOTOPMOST = -2;

 //设置此窗体为活动窗体:
 //将创建指定窗口的线程带到前台并激活该窗口。键盘输入直接指向窗口,并为用户更改各种视觉提示。
 //系统为创建前台窗口的线程分配的优先级略高于其他线程。
 [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
 public static extern bool SetForegroundWindow(IntPtr hWnd);

 //设置此窗体为活动窗体:
 //激活窗口。窗口必须附加到调用线程的消息队列。
 [DllImport("user32.dll", EntryPoint = "SetActiveWindow")]
 public static extern IntPtr SetActiveWindow(IntPtr hWnd);

 //设置窗体位置
 [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);

2、函数调用(放在构造或Load)

csharp 复制代码
 // 设置窗体显示在最上层
 SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010 | 0x0080);
 // 设置本窗体为活动窗体
 SetActiveWindow(this.Handle);
 SetForegroundWindow(this.Handle);
 // 设置窗体置顶
  this.TopMost = true;

文章抄录自 c#让窗体永在最前 调用windows api 将窗体设为topmost

相关推荐
逆向编程12 分钟前
QGIS地图投影(CRS坐标参照系)设置
开发语言
free-elcmacom19 分钟前
发际线警告!手撕《C陷阱与缺陷》终极 BOSS:signal 函数与 typedef 魔法
c语言·开发语言·visual studio code·visual studio
程序员阿鹏19 分钟前
简单介绍一下软引用
java·开发语言·jvm·数据结构·后端
SL_staff32 分钟前
项目延期时如何用四类角色机制实现责任锚定?一线技术实践拆解
java·低代码·团队管理
wno70436 分钟前
Spring Security Session管理
java·后端·spring
吃饱了得干活37 分钟前
主键选择:从单机到分布式,一个被低估的性能决策
java·后端
haluhalu.42 分钟前
初识 Protobuf:微服务跨语言通信的一份契约
java·c语言·开发语言·c++·python
xcl09251 小时前
宠物商城社交托运购物系统开发实战:从架构设计到完整实现指南
java·spring boot
孙启超1 小时前
【AI开发之Rust】第 12 课:并发模型与同步原语
开发语言·后端·rust
没差c1 小时前
RetrofitClient写接口、导包、配置、路径
java·spring boot·okhttp