开发者

How to get handles to all windows of another application

in my application i have timer, in TimerProc i want to get handles of all windows(main and child) of the another application th开发者_JS百科at has focus. I have no idea how to do that because i don't understand functions like GetNextWindow or GetParent and Z-oder of windows and i can't find anywhere very detailed explanation of how this functions works(i dont understand explanation on msdn). Please can you give me some advice or block of code which do that? Many thanks for answer.


Use GetForegroundWindow() function - it returns the HWND of the window the user currently is working with. Then having this handle you can retrieve childs in such a way:

 HWND a_hWnd = (HWND)hParent;
  HWND a_FirstChild = NULL;
  a_FirstChild = ::GetWindow(a_hWnd, GW_CHILD);

  if (a_FirstChild != NULL)
  {

    HWND a_NextChild = NULL;
    do
    {
      a_NextChild = ::GetWindow(a_FirstChild, GW_HWNDNEXT);
      if (a_NextChild != NULL)
      {
        a_FirstChild = a_NextChild;
      }
    }
    while (a_NextChild != NULL);
}


GetForeGroundWindow to get the current foreground window/dialog
GetParent until you get NULL (that gets you to the top level window)**
EnumChildWindows to get to all the dependent windows

** Note that an application can have more than one top level window, though this isn't usual.

Code:

void Ccpp_testDlg::DoWalk ()
{
   HWND hCurrent;
   HWND hNew;

   hCurrent = ::GetForegroundWindow ();
   hNew     = hCurrent;

   while (hNew != NULL)
   {
      hNew = ::GetParent (hCurrent);
      if (hNew != NULL) 
      {
         hCurrent = hNew;
      }
   }
   EnumChildWindows (hCurrent, EnumProc, 0);
}

BOOL CALLBACK EnumProc (HWND hwnd,LPARAM lParam)
{
   TCHAR szText [MAX_PATH];
   GetWindowText (hwnd, szText, sizeof(szText));
   // do something with text
   return TRUE;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜