C# 获取指定进程的主窗口句柄
2012-04-25 16:26
549 查看
静态方法,直接上代码吧:
using System; using System.Runtime.InteropServices; namespace Macroresolute { public static class ProcessEx { private static class NativeMethods { internal const uint GW_OWNER = 4; internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("User32.dll", CharSet = CharSet.Auto)] internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("User32.dll", CharSet = CharSet.Auto)] internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId); [DllImport("User32.dll", CharSet = CharSet.Auto)] internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); [DllImport("User32.dll", CharSet = CharSet.Auto)] internal static extern bool IsWindowVisible(IntPtr hWnd); } public static IntPtr GetMainWindowHandle(int processId) { IntPtr MainWindowHandle = IntPtr.Zero; NativeMethods.EnumWindows(new NativeMethods.EnumWindowsProc((hWnd, lParam) => { IntPtr PID; NativeMethods.GetWindowThreadProcessId(hWnd, out PID); if (PID == lParam && NativeMethods.IsWindowVisible(hWnd) && NativeMethods.GetWindow(hWnd, NativeMethods.GW_OWNER) == IntPtr.Zero) { MainWindowHandle = hWnd; return false; } return true; }), new IntPtr(processId)); return MainWindowHandle; } } }
相关文章推荐
- 获取指定窗口句柄结束进程 OpenProcess FindWindow GetExitCodeprocess TerminateProcess
- C#获取进程的主窗口句柄的实现方法
- [转载]C#获取进程的主窗口句柄
- 怎么用C#获取指定窗口的句柄(笔记)
- C#获取进程的主窗口句柄
- C#获取进程的主窗口句柄
- C#遍历进程获取主窗口句柄
- C#获取进程的主窗口句柄的实现方法
- C#获取进程的主窗口句柄
- [转]C#获取窗口进程ID与句柄还有读写内存类
- (转)获取指定进程的主窗口句柄
- C#遍历进程获取主窗口句柄
- C#获取当前活动窗口进程、进程ID、程序路径
- C++根据进程获取主窗口的句柄
- C# 通过窗口句柄获取程序运行路径 通过程序运行路径获取程序图标
- WIN API-VFP获取第三方窗体的控件信息(句柄,窗口ID,进程ID,类名,标题
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
- 在DLL中获取主进程窗口句柄
- C#获取鼠标位置窗口句柄