C#获取进程的主窗口句柄的实现方法
通过调用Win32 API实现。 复制代码 代码如下: public class User32API { private static Hashtable processWnd = null; public delegate bool WNDENUMPROC(IntPtr hwnd,uint lParam); static User32API() [DllImport("user32.dll",EntryPoint = "EnumWindows",SetLastError = true)] [DllImport("user32.dll",EntryPoint = "GetParent",SetLastError = true)] [DllImport("user32.dll",EntryPoint = "GetWindowThreadProcessId")] [DllImport("user32.dll",EntryPoint = "IsWindow")] [DllImport("kernel32.dll",EntryPoint = "SetLastError")] public static IntPtr GetCurrentWindowHandle() if (objWnd != null) bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc),uiPid); return ptrWnd; private static bool EnumWindowsProc(IntPtr hwnd,uint lParam) if (GetParent(hwnd) == IntPtr.Zero) return true; 调用User32API.GetCurrentWindowHandle()即可返回当前进程的主窗口句柄,如果获取失败则返回IntPtr.Zero。 --EOF-- (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |