Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. WindowSearch.h
  5. Abstract:
  6. Author:
  7. Hakki T. Bostanci (hakkib) 06-Apr-2000
  8. Revision History:
  9. --*/
  10. #ifndef _WINDOWSEARCH_H_
  11. #define _WINDOWSEARCH_H_
  12. //////////////////////////////////////////////////////////////////////////
  13. //
  14. //
  15. //
  16. class CWindowSearch
  17. {
  18. public:
  19. CWindowSearch()
  20. {
  21. m_nMatches = -1;
  22. //m_hWnd = 0;
  23. }
  24. friend HWND FindChildWindow(HWND hWnd, CWindowSearch &rWindowSearch)
  25. {
  26. rWindowSearch.m_nMatches = 0;
  27. EnumChildWindows(hWnd, EnumProc, (LPARAM) &rWindowSearch);
  28. return rWindowSearch.Result();
  29. }
  30. friend HWND WaitForChildWindow(HWND hWnd, CWindowSearch &rWindowSearch, HANDLE hBreak)
  31. {
  32. rWindowSearch.m_nMatches = 0;
  33. do
  34. {
  35. EnumChildWindows(hWnd, EnumProc, (LPARAM) &rWindowSearch);
  36. }
  37. while (rWindowSearch.m_nMatches == 0 && WaitForSingleObject(hBreak, 250) == WAIT_TIMEOUT);
  38. return rWindowSearch.Result();
  39. }
  40. friend HWND FindThreadWindow(DWORD dwThreadId, CWindowSearch &rWindowSearch)
  41. {
  42. rWindowSearch.m_nMatches = 0;
  43. EnumThreadWindows(dwThreadId, EnumProc, (LPARAM) &rWindowSearch);
  44. return rWindowSearch.Result();
  45. }
  46. friend HWND WaitForThreadWindow(DWORD dwThreadId, CWindowSearch &rWindowSearch, HANDLE hBreak)
  47. {
  48. rWindowSearch.m_nMatches = 0;
  49. do
  50. {
  51. EnumThreadWindows(dwThreadId, EnumProc, (LPARAM) &rWindowSearch);
  52. }
  53. while (rWindowSearch.m_nMatches == 0 && WaitForSingleObject(hBreak, 250) == WAIT_TIMEOUT);
  54. return rWindowSearch.Result();
  55. }
  56. HWND Result() const
  57. {
  58. switch (m_nMatches)
  59. {
  60. case -1:
  61. SetLastError(ERROR_NOT_FOUND);//ERROR_NOT_STARTED);
  62. return 0;
  63. case 0:
  64. SetLastError(ERROR_NOT_FOUND);
  65. return 0;
  66. case 1:
  67. return m_hWnd;
  68. default:
  69. SetLastError(ERROR_NOT_FOUND);//ERROR_MULTIPLE_FOUND);
  70. return 0;
  71. }
  72. }
  73. private:
  74. virtual BOOL TestWindow(HWND hWnd) = 0;
  75. static BOOL CALLBACK EnumProc(HWND hWnd, LPARAM lParam)
  76. {
  77. return ((CWindowSearch *)lParam)->TestWindow(hWnd);
  78. }
  79. protected:
  80. int m_nMatches;
  81. HWND m_hWnd;
  82. };
  83. //////////////////////////////////////////////////////////////////////////
  84. //
  85. //
  86. //
  87. class CWindowSearchByText : public CWindowSearch
  88. {
  89. public:
  90. explicit CWindowSearchByText(PCTSTR pszText)
  91. {
  92. m_pszText = pszText;
  93. }
  94. virtual BOOL TestWindow(HWND hWnd)
  95. {
  96. if (_tcscmp(CWindowText(hWnd), m_pszText) == 0)
  97. {
  98. ++m_nMatches;
  99. m_hWnd = hWnd;
  100. }
  101. return TRUE;
  102. }
  103. private:
  104. PCTSTR m_pszText;
  105. };
  106. //////////////////////////////////////////////////////////////////////////
  107. //
  108. //
  109. //
  110. class CWindowSearchByClass : public CWindowSearch
  111. {
  112. public:
  113. explicit CWindowSearchByClass(PCTSTR pszClass)
  114. {
  115. m_pszClass = pszClass;
  116. }
  117. virtual BOOL TestWindow(HWND hWnd)
  118. {
  119. if (_tcscmp(CClassName(hWnd), m_pszClass) == 0)
  120. {
  121. ++m_nMatches;
  122. m_hWnd = hWnd;
  123. }
  124. return TRUE;
  125. }
  126. private:
  127. PCTSTR m_pszClass;
  128. };
  129. //////////////////////////////////////////////////////////////////////////
  130. //
  131. //
  132. //
  133. class CWindowSearchById : public CWindowSearch
  134. {
  135. public:
  136. explicit CWindowSearchById(int nId)
  137. {
  138. m_nId = nId;
  139. }
  140. virtual BOOL TestWindow(HWND hWnd)
  141. {
  142. if (GetDlgCtrlID(hWnd) == m_nId)
  143. {
  144. ++m_nMatches;
  145. m_hWnd = hWnd;
  146. }
  147. return TRUE;
  148. }
  149. private:
  150. int m_nId;
  151. };
  152. //////////////////////////////////////////////////////////////////////////
  153. //
  154. //
  155. //
  156. inline BOOL PushButton(HWND hWnd)
  157. {
  158. return
  159. PostMessage(hWnd, WM_LBUTTONDOWN, 0, 0) &&
  160. PostMessage(hWnd, WM_LBUTTONUP, 0, 0);
  161. }
  162. //////////////////////////////////////////////////////////////////////////
  163. //
  164. //
  165. //
  166. inline BOOL SetText(HWND hWnd, PCTSTR pText)
  167. {
  168. return SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM) pText);
  169. }
  170. #endif //_WINDOWSEARCH_H_