ExternalWindowTracker.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "stdafx.h"
  2. #include "externalwindowtracker.h"
  3. ExternalWindowTracker::ExternalWindowTracker(void)
  4. {
  5. m_activeWnd = NULL;
  6. m_focusWnd = NULL;
  7. m_iHaveFocus = false;
  8. }
  9. ExternalWindowTracker::~ExternalWindowTracker(void)
  10. {
  11. }
  12. bool ExternalWindowTracker::IsAppWnd( HWND hWnd )
  13. {
  14. DWORD dwMyPID = ::GetCurrentProcessId();
  15. DWORD dwTestPID;
  16. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  17. return dwMyPID == dwTestPID;
  18. }
  19. bool ExternalWindowTracker::TrackActiveWnd()
  20. {
  21. BOOL fromHook = true;
  22. HWND newFocus = NULL;
  23. HWND newActive = ::GetForegroundWindow();
  24. GUITHREADINFO guiThreadInfo;
  25. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  26. DWORD OtherThreadID = GetWindowThreadProcessId(newActive, NULL);
  27. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  28. {
  29. newFocus = guiThreadInfo.hwndFocus;
  30. }
  31. if(newFocus == 0 || !IsWindow(newFocus) || newActive == 0 || !IsWindow(newActive))
  32. {
  33. //Log(_T("TargetActiveWindow values invalid"));
  34. return false;
  35. }
  36. if(IsAppWnd(newFocus) || IsAppWnd(newActive))
  37. {
  38. if(m_iHaveFocus == false)
  39. {
  40. }
  41. m_iHaveFocus = true;
  42. return false;
  43. }
  44. m_focusWnd = newFocus;
  45. m_activeWnd = newActive;
  46. m_iHaveFocus = false;
  47. return true;
  48. }
  49. CString ExternalWindowTracker::WndName(HWND hWnd)
  50. {
  51. TCHAR cWindowText[200];
  52. HWND hParent = hWnd;
  53. ::GetWindowText(hParent, cWindowText, 100);
  54. int nCount = 0;
  55. while(wcslen(cWindowText) <= 0)
  56. {
  57. hParent = ::GetParent(hParent);
  58. if(hParent == NULL)
  59. break;
  60. ::GetWindowText(hParent, cWindowText, 100);
  61. nCount++;
  62. if(nCount > 100)
  63. {
  64. //Log(_T("GetTargetName reached maximum search depth of 100"));
  65. break;
  66. }
  67. }
  68. return cWindowText;
  69. }