Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. namespace WindowSearch
  2. {
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. /// <summary>
  8. /// Window Finding Utilities
  9. /// </summary>
  10. internal class FindWindowLike
  11. {
  12. private const int GWLID = -12;
  13. private const int GWHWNDNEXT = 2;
  14. private const int GWCHILD = 5;
  15.  
  16. /// <summary>
  17. /// Find all windows matching a given title and class
  18. /// </summary>
  19. /// <param name="hwndStart">Beginning hwnd to start searching at</param>
  20. /// <param name="findText">Window title to search</param>
  21. /// <param name="findClassName">Class name to search</param>
  22. /// <returns>Returns an array of windows that matches the given arguments</returns>
  23. public static Window[] Find(int hwndStart, string findText, string findClassName)
  24. {
  25. ArrayList windows = DoSearch(hwndStart, findText, findClassName);
  26.  
  27. return (Window[])windows.ToArray(typeof(Window));
  28. }
  29.  
  30. private static ArrayList DoSearch(int hwndStart, string findText, string findClassName)
  31. {
  32. ArrayList list = new ArrayList();
  33.  
  34. if (hwndStart == 0)
  35. {
  36. hwndStart = GetDesktopWindow();
  37. }
  38.  
  39. int hwnd = GetWindow(hwndStart, GWCHILD);
  40.  
  41. while (hwnd != 0)
  42. {
  43. // Recursively search for child windows.
  44. list.AddRange(DoSearch(hwnd, findText, findClassName));
  45.  
  46. StringBuilder text = new StringBuilder(255);
  47. int rtn = GetWindowText(hwnd, text, 255);
  48. string windowText = text.ToString();
  49. windowText = windowText.Substring(0, rtn);
  50.  
  51. StringBuilder cls = new StringBuilder(255);
  52. rtn = GetClassName(hwnd, cls, 255);
  53. string className = cls.ToString();
  54. className = className.Substring(0, rtn);
  55.  
  56. if (GetParent(hwnd) != 0)
  57. {
  58. rtn = GetWindowLong(hwnd, GWLID);
  59. }
  60.  
  61. if (windowText.Length > 0 && windowText.StartsWith(findText) &&
  62. (className.Length == 0 || className.StartsWith(findClassName)))
  63. {
  64. Window currentWindow = new Window
  65. {
  66. Title = windowText,
  67. Class = className,
  68. Handle = hwnd
  69. };
  70.  
  71. list.Add(currentWindow);
  72. }
  73.  
  74. hwnd = GetWindow(hwnd, GWHWNDNEXT);
  75. }
  76.  
  77. return list;
  78. }
  79.  
  80. [DllImport("user32")]
  81. private static extern int GetWindow(int hwnd, int wCmd);
  82.  
  83. [DllImport("user32")]
  84. private static extern int GetDesktopWindow();
  85.  
  86. [DllImport("user32", EntryPoint = "GetWindowLongA")]
  87. private static extern int GetWindowLong(int hwnd, int nIndex);
  88.  
  89. [DllImport("user32")]
  90. private static extern int GetParent(int hwnd);
  91.  
  92. [DllImport("user32", EntryPoint = "GetClassNameA")]
  93. private static extern int GetClassName(
  94. int hWnd, [Out] StringBuilder lpClassName, int nMaxCount);
  95.  
  96. [DllImport("user32", EntryPoint = "GetWindowTextA")]
  97. private static extern int GetWindowText(
  98. int hWnd, [Out] StringBuilder lpString, int nMaxCount);
  99.  
  100. /// <summary>
  101. /// Window Parameters
  102. /// </summary>
  103. internal class Window
  104. {
  105. /// <summary>
  106. /// Gets or sets window title
  107. /// </summary>
  108. internal string Title { get; set; }
  109.  
  110. /// <summary>
  111. /// Gets or sets class name
  112. /// </summary>
  113. internal string Class { get; set; }
  114.  
  115. /// <summary>
  116. /// Gets or sets handle
  117. /// </summary>
  118. internal int Handle { get; set; }
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement