C# 获取指定进程的主窗口句柄
静态方法,直接上代码吧:
1 using System; 2 using System.Runtime.InteropServices; 3 4 namespace Macroresolute 5 { 6 public static class ProcessEx 7 { 8 private static class NativeMethods 9 { 10 internal const uint GW_OWNER = 4; 11 12 internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); 13 14 [DllImport("User32.dll", CharSet = CharSet.Auto)] 15 internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); 16 17 [DllImport("User32.dll", CharSet = CharSet.Auto)] 18 internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId); 19 20 [DllImport("User32.dll", CharSet = CharSet.Auto)] 21 internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); 22 23 [DllImport("User32.dll", CharSet = CharSet.Auto)] 24 internal static extern bool IsWindowVisible(IntPtr hWnd); 25 } 26 27 public static IntPtr GetMainWindowHandle(int processId) 28 { 29 IntPtr MainWindowHandle = IntPtr.Zero; 30 31 NativeMethods.EnumWindows(new NativeMethods.EnumWindowsProc((hWnd, lParam) => 32 { 33 IntPtr PID; 34 NativeMethods.GetWindowThreadProcessId(hWnd, out PID); 35 36 if (PID == lParam && 37 NativeMethods.IsWindowVisible(hWnd) && 38 NativeMethods.GetWindow(hWnd, NativeMethods.GW_OWNER) == IntPtr.Zero) 39 { 40 MainWindowHandle = hWnd; 41 return false; 42 } 43 44 return true; 45 46 }), new IntPtr(processId)); 47 48 return MainWindowHandle; 49 } 50 } 51 }52
转载于:https://www.cnblogs.com/zys529/archive/2012/04/25/2470039.html
C# 获取指定进程的主窗口句柄相关推荐
- 如何获得指定进程的主窗口
我们知道一个进程可能是没有主窗口(比如系统的服务进程)而有的进程可能又拥有不止一个的主窗口(比如OutLook软件),那么我们该如何才能取得指定进程的所有主窗口呢? 我们必须明确我们感兴趣" ...
- 获取指定进程所对应的可执行(EXE)文件全路径(代码)
1 #include "Psapi.h" 2 #pragma comment(lib, "Psapi.lib ")//需要包含头文件哦! 1 /* 功 能:获取 ...
- 获取指定进程所占用内存、指定GPU显存信息、所有GPU型号和驱动
######################################################### # 获取指定进程所占用内存 ############################ ...
- 获取指定进程的加载基址
背景 之前,自己写过一个进程内存分析的小程序,其中,就有一个功能是获取进程在内存中的加载基址.由于现在Windows系统引入了ASLR (Address Space Layout Randomizat ...
- Shell命令之ps获取指定进程的进程号
直接看代码 ps aux|grep upload2FTP.sh|grep -v "grep"|awk '{print $2}' 其中awk指定输出第二列内容(centos对应进程号 ...
- 利用Linux的/proc/stat获取指定进程的cpu占有率及内存
利用Linux的 /proc/stat计算进程的cpu占有率和内存 计算的是进程多个cpu的平均占有率,并不是每个cpu的,所以和top命令中的不一样 getinfo.cpp 1 #include&l ...
- php获取pid,在Shell脚本中获取指定进程的PID
NodeJS:Error: Cannot find module 'jshint/src/cli' 以前命令:npm install gulp-jshint --sav ...
- linux c 获取指定进程 父进程pid
示例如下: int GetFatherPid(int pid) {char dir[1024]={0};char path[1024] = {0};char buf[1024] = {0};int r ...
- java 获取进程_java获取指定进程的stat
读取/proc/{pid}/stat /** * Get CPU stat of specified process. * @param pid Process ID * @return null m ...
最新文章
- Invalid icon file 原因
- Linux的cp -a与cp -p
- volatile指令重排_有多少人面试栽到Volatile上?面试问题都总结到这儿了
- centos上安装jdk
- 如何快速的解决Maven依赖冲突
- Java循环删除集合多个元素的正确打开方式
- 强制卸载软件包linux,强制删除rpm包的方法
- 虽说下班时间自己说了算
- 达“超人”水平?强化学习得这么用!
- python之路_kindEditor编辑器及beautifulsoup模块
- 第七章、epub文件处理 -- 解析 .xhtml文件 (一)
- Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute
- wien10 / win11双开微信
- 计算机中的查找快捷键,Excel搜索快捷键如何在excel中快速找到所需信息
- 万用表怎么测量电池容量_万用表如何测量电池容量?
- 在绩效评估中使用 360 反馈
- 三星android文件传输,最好的三星Galaxy S8管理器:如何将文件传输到三星Galaxy S8...
- Win7+Ubuntu双系统安装教程
- 用 TensorFlow.js 在浏览器中训练一个计算机视觉模型(手写数字分类器)
- java-hiho1000A+B