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

相关推荐
workflower3 小时前
单元测试-例子
java·开发语言·算法·django·个人开发·结对编程
YuanlongWang3 小时前
C# 基础——装箱和拆箱
java·开发语言·c#
b78gb3 小时前
电商秒杀系统设计 Java+MySQL实现高并发库存管理与订单处理
java·开发语言·mysql
wb043072014 小时前
性能优化实战:基于方法执行监控与AI调用链分析
java·人工智能·spring boot·语言模型·性能优化
LXS_3574 小时前
Day 05 C++ 入门 之 指针
开发语言·c++·笔记·学习方法·改行学it
天若有情6735 小时前
Java Swing 实战:从零打造经典黄金矿工游戏
java·后端·游戏·黄金矿工·swin
etsuyou6 小时前
js前端this指向规则
开发语言·前端·javascript
lichong9516 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
shizhenshide6 小时前
为什么有时候 reCAPTCHA 通过率偏低,常见原因有哪些
开发语言·php·验证码·captcha·recaptcha·ezcaptcha
lichong9516 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++