C# EnumWindows 枚举所有窗口并返回指定窗口标题句柄
//调用 ListTitleHandle = new List<IntPtr>(); var r = EnumWindows(PrintWindow, "计算器"); //函数指针类型 delegate bool deleWindowsProc(IntPtr hWnd, string lParam); [DllImport("user32.dll")] static extern int EnumWindows(deleWindowsProc hWnd, string lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount); //存储句柄集合 List<IntPtr> ListTitleHandle; //回调函数 bool PrintWindow(IntPtr hWnd, string lParam) { //分配空间 var sb = new StringBuilder(50); GetWindowText(hWnd, sb, sb.Capacity); //注意某些窗口没有标题 if (sb.ToString() != String.Empty && sb.ToString().Equals(lParam)) { Console.WriteLine(sb.ToString()); ListTitleHandle.Add(hWnd); } //返回True继续往下回调,返回False终止 return true; }