很老的项目中的代码,现在也想不起来为什么要这么写了。
[DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public extern static int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("User32.dll ")] public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText); [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam); public delegate bool CallBack(IntPtr hwnd, int lParam); /// <summary> /// 根据父窗口的标题和子窗口的类名返回子窗口的句柄 /// </summary> /// <param name="HwndName">子窗口的类名</param> /// <param name="WindowName">父窗口的标题</param> /// <returns></returns> public static IntPtr FindWindowExOrde(string HwndName, string WindowName) { IntPtr HwndId = new IntPtr(); IntPtr hwnd = FindWindow(null, WindowName); IntPtr ChildHwndWindow = FindWindowEx(hwnd, IntPtr.Zero, null, null); var H = EnumChildWindows(hwnd, (x, l) => { StringBuilder sb = new StringBuilder(HwndName.Length + 2); if (x != IntPtr.Zero) { GetClassName(x, sb, sb.Capacity); if (HwndName.Equals(sb.ToString())) { HwndId = x; return true; } } else return false; return true; }, 0); return HwndId; }