C#如何让窗体永远在窗体最前面显示

C# 窗体永远在最前

1、调用系统API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)

 // 设置窗体显示在最上层
 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

有可能导致其他软件按不了按钮 请看方法2

posted @   hack747  阅读(2374)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示