微软实现的获取进程主窗口句柄代码
2010-12-04 16:29
447 查看
public class MyProcess { private bool haveMainWindow = false; private IntPtr mainWindowHandle = IntPtr.Zero; private int processId = 0; private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); public IntPtr GetMainWindowHandle(int processId) { if (!this.haveMainWindow) { this.mainWindowHandle = IntPtr.Zero; this.processId = processId; EnumThreadWindowsCallback callback = new EnumThreadWindowsCallback(this.EnumWindowsCallback); EnumWindows(callback, IntPtr.Zero); GC.KeepAlive(callback); this.haveMainWindow = true; } return this.mainWindowHandle; } private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter) { int num; GetWindowThreadProcessId(new HandleRef(this, handle), out num); if ((num == this.processId) && this.IsMainWindow(handle)) { this.mainWindowHandle = handle; return false; } return true; } private bool IsMainWindow(IntPtr handle) { return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle))); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool IsWindowVisible(HandleRef hWnd); }
相关文章推荐
- 转:VC:如何根据文件名来获取程序进程和窗口句柄的代码(转载)
- 通过进程ID获取窗口句柄(微软)
- C#获取进程的主窗口句柄的实现方法
- C#获取进程的主窗口句柄的实现方法
- [转载]C#获取进程的主窗口句柄
- 由进程获取窗口(CMainframe)句柄
- 根据窗口句柄来获取进程ID
- js获取元素相对窗口位置的实现代码
- js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码
- C#获取进程的主窗口句柄
- 分析获取当前系统进程列表的代码流程(及ioctl在bionic库中的实现)
- 通过进程exe名称获取窗口句柄
- Delphi 通得进程ID获取主窗口句柄
- Linux C 获取进程退出值的实现代码
- C#获取进程的主窗口句柄
- JS获取屏幕,浏览器窗口大小,网页高度宽度(实现代码)
- 通过进程id获取窗口句柄
- C#获取进程的主窗口句柄
- js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码