枚举窗口及其应用

来源:互联网 发布:社会网络的同义词 编辑:程序博客网 时间:2024/06/05 15:58

一:利用GetWindow(ParentWnd, GW_CHILD);

[cpp] view plain copy
  1. void FindAllChildWnd(HWND ParentWnd)  
  2. {  
  3.  HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);  
  4.  for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))  
  5.  {  
  6.   TCHAR WindowText[30]={0};  
  7.   ::SendMessage(hChild,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  8.   TCHAR ClassName[30]={0};  
  9.   ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));  
  10.   trace2(WindowText,ClassName);  
  11.   FindAllChildWnd(hChild);  
  12.  }  
  13. }  
  14. void CDemoDlg::OnButton1()   
  15. {  
  16.  FindAllChildWnd(::GetForegroundWindow());  
  17. }  
 

 

二:利用回调函数EnumChildProc枚举所有子窗口

[cpp] view plain copy
  1. BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)     
  2. {  
  3.  TCHAR WindowText[100]={0};  
  4.  ::SendMessage(hwnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  5.  TCHAR ClassName[100]={0};  
  6.  ::GetClassName(hwnd,ClassName,sizeof(ClassName)/sizeof(TCHAR));  
  7.  trace2(WindowText,ClassName);  
  8.  return TRUE ;    
  9. }  
  10. void CDemoDlg::OnButton2()   
  11. {  
  12.  ::EnumChildWindows(::GetForegroundWindow(),EnumChildProc,NULL);  
  13. }  
 

 

三:利用回调函数EnumChildProc枚举所有可见的桌面窗口

[cpp] view plain copy
  1. BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)  
  2. {  
  3.  if ( GetParent(hwnd)==NULL  &&  IsWindowVisible(hwnd) )  
  4.  {  
  5.   TCHAR WindowText[100]={0};  
  6.   ::SendMessage(hwnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  7.   trace(WindowText);  
  8.  }  
  9.  return true;     
  10. }  
  11.    
  12. void CDemoDlg::OnButton3()   
  13. {  
  14.  EnumWindows(EnumWindowsProc ,(LPARAM)0 );  
  15. }  
 

 

四:利用FindWindowEx寻找指定类名或窗口标题的窗口句柄

[cpp] view plain copy
  1. //简单的寻找子窗口句柄  
  2.  HWND hwnd=::FindWindowEx(m_hWnd,NULL,"#32770",NULL);   
 

 


2.枚举窗口的应用

 

应用一:寻找指定类名的窗口句柄。

(1)单一函数的形式

[cpp] view plain copy
  1. /**************************************************************************** 
  2. 寻找指定类名的窗口句柄 方法一 
  3. ****************************************************************************/  
  4. HWND FindWithClassName(HWND ParentWnd,TCHAR* FindClassName)  
  5. {  
  6.  HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);  
  7.  for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))  
  8.  {  
  9.   TCHAR ClassName[100]={0};  
  10.   ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));  
  11.   if (_tcscmp(ClassName,FindClassName)==0)  
  12.    return hChild;  
  13.     
  14.   HWND FindWnd=FindWithClassName(hChild,FindClassName);  
  15.   if (FindWnd)  
  16.    return FindWnd;  
  17.  }  
  18.  return NULL;  
  19. }  
  20. //调用  
  21. void CDemoDlg::OnButton6()   
  22. {  
  23.  HWND hWnd=FindWithClassName(::GetForegroundWindow(),"Edit");  
  24.  if (hWnd)  
  25.  {  
  26.   TCHAR WindowText[100]={0};  
  27.   ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  28.   trace(WindowText);  
  29.  }  
  30.  else  
  31.  {  
  32.   trace("can not find");  
  33.  }  
  34. }  
 

(2)类的形式

[cpp] view plain copy
  1. /**************************************************************************** 
  2. 寻找指定类名的窗口句柄 方法二 
  3. ****************************************************************************/  
  4. class CFindWithClassName  
  5. {  
  6. public:  
  7.  static HWND FindWnd;  
  8.  static TCHAR ClassName[100];  
  9.  static BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)     
  10.  {  
  11.   ::GetClassName(hwnd,ClassName,sizeof(ClassName)/sizeof(TCHAR));    
  12.   if (_tcscmp(ClassName,(TCHAR*)lParam)==0)  
  13.   {  
  14.    FindWnd=hwnd;  
  15.    return false;  
  16.   }    
  17.   return TRUE ;    
  18.  }  
  19.  static HWND Find(HWND hwndParent, TCHAR* FindClassName)  
  20.  {  
  21.   ::EnumChildWindows(hwndParent,EnumChildProc,(LPARAM)FindClassName);   
  22.   return FindWnd;  
  23.  }  
  24. };  
  25. HWND CFindWithClassName::FindWnd=NULL;  
  26. TCHAR CFindWithClassName::ClassName[100]=_T("");  
  27. //调用  
  28. void CDemoDlg::OnButton7()   
  29. {  
  30.  HWND hWnd=CFindWithClassName::Find(::GetForegroundWindow(),_T("Edit"));  
  31.  if (hWnd)  
  32.  {  
  33.   TCHAR WindowText[100]={0};  
  34.   ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  35.   trace(WindowText);  
  36.  }  
  37.  else  
  38.  {  
  39.   trace("can not find");  
  40.  }  
  41. }  
 

 

(3)大师Paul DiLascia写的。

[cpp] view plain copy
  1. ////////////////////////////////////////////////////////////////  
  2. // MSDN Magazine -- August 2003  
  3. // If this code works, it was written by Paul DiLascia.  
  4. // If not, I don't know who wrote it.  
  5. // Compiles with Visual Studio .NET on Windows XP. Tab size=3.  
  6. //  
  7. // ---  
  8. // This class encapsulates the process of finding a window with a given class name  
  9. // as a descendant of a given window. To use it, instantiate like so:  
  10. //  
  11. //  CFindWnd fw(hwndParent,classname);  
  12. //  
  13. // fw.m_hWnd will be the HWND of the desired window, if found.  
  14. //  
  15. class CFindWnd {  
  16. private:  
  17.  //////////////////  
  18.  // This private function is used with EnumChildWindows to find the child  
  19.  // with a given class name. Returns FALSE if found (to stop enumerating).  
  20.  //  
  21.  static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {  
  22.   CFindWnd *pfw = (CFindWnd*)lParam;  
  23.   HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);  
  24.   if (hwnd) {  
  25.    pfw->m_hWnd = hwnd; // found: save it  
  26.    return FALSE;   // stop enumerating  
  27.   }  
  28.   EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse  
  29.   return TRUE;    // keep looking  
  30.  }  
  31. public:  
  32.  LPCSTR m_classname;   // class name to look for  
  33.  HWND m_hWnd;     // HWND if found  
  34.  // ctor does the work--just instantiate and go  
  35.  CFindWnd(HWND hwndParent, LPCSTR classname)  
  36.   : m_hWnd(NULL), m_classname(classname)  
  37.  {  
  38.   FindChildClassHwnd(hwndParent, (LPARAM)this);  
  39.  }  
  40. };  
  41. void CDemoDlg::OnButton5()   
  42. {  
  43.  CFindWnd fw(::GetForegroundWindow(),"Edit");  
  44.  TCHAR WindowText[30]={0};  
  45.  ::SendMessage(fw.m_hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  46.  trace(WindowText);  
  47. }  
 

 

 

 

应用二:寻找指定控件ID的窗口句柄。


子控件的ID使用spy++(VS2008版本) 查看获得

 

(1)方法一,类的形式

[cpp] view plain copy
  1. class CFindWithID  
  2. {  
  3. public:  
  4.  static BOOL CALLBACK _EnumChildProc( HWND hwnd, LPARAM lParam )    
  5.  {    
  6.   //*(DWORD*)lParam 即是传进来的Control ID  
  7.   if ( GetDlgCtrlID(hwnd) == *(DWORD*)lParam  )//判断是否为需要的控件    
  8.   {    
  9.    *(HWND*)lParam=hwnd; //强制转换  
  10.    return FALSE;    
  11.   }    
  12.   return TRUE;    
  13.  }    
  14.    
  15.  static HWND Find(HWND TopWnd, DWORD ControlID )    
  16.  {    
  17.   EnumChildWindows( TopWnd , _EnumChildProc, (LPARAM)&ControlID );    
  18.   return ::IsWindow( (HWND)ControlID ) ? (HWND)ControlID : NULL;  
  19.  }   
  20. };  
  21. void CDemoDlg::OnButton8()   
  22. {  
  23.  HWND hWnd=CFindWithID::Find(::GetForegroundWindow(),0x000003e9);  
  24.  if (hWnd)  
  25.  {  
  26.   TCHAR WindowText[100]={0};  
  27.   ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  28.   trace(WindowText);  
  29.  }  
  30.  else  
  31.  {  
  32.   trace("can not find");  
  33.  }   
  34. }  
 

 

(2)方法二:(单一函数的形式)

[cpp] view plain copy
  1. HWND FindControlWnd(HWND ParentWnd,DWORD ControlID)  
  2. {  
  3.  HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);  
  4.  for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))  
  5.  {  
  6.   //判断是否为需要的控件    
  7.   if ( GetDlgCtrlID(hChild) == ControlID  )  
  8.    return hChild;   
  9.   HWND FindWnd=FindControlWnd(hChild,ControlID);  
  10.   if (FindWnd)  
  11.    return FindWnd;  
  12.  }  
  13.  return NULL;  
  14. }  
  15. void CDemoDlg::OnButton9()   
  16. {  
  17.  HWND hWnd=FindControlWnd(::GetForegroundWindow(),0x000003e9);  
  18.  if (hWnd)  
  19.  {  
  20.   TCHAR WindowText[100]={0};  
  21.   ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);   
  22.   trace(WindowText);  
  23.  }  
  24.  else  
  25.  {  
  26.   trace("can not find");  
  27.  }    
  28. }  
0 0
原创粉丝点击