现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

枚举窗口及其应用

2012-08-21 05:42 工业·编程 ⁄ 共 5923字 ⁄ 字号 暂无评论

1.下面请看几种枚举窗口的方式

一:利用GetWindow(ParentWnd, GW_CHILD);

void FindAllChildWnd(HWND ParentWnd)
{
HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);
for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
{
  TCHAR WindowText[30]={0};
  ::SendMessage(hChild,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  TCHAR ClassName[30]={0};
  ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));
  trace2(WindowText,ClassName);
  FindAllChildWnd(hChild);
}
}
void CDemoDlg::OnButton1()
{
FindAllChildWnd(::GetForegroundWindow());
}

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

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)  
{
TCHAR WindowText[100]={0};
::SendMessage(hwnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
TCHAR ClassName[100]={0};
::GetClassName(hwnd,ClassName,sizeof(ClassName)/sizeof(TCHAR));
trace2(WindowText,ClassName);
return TRUE ; 
}
void CDemoDlg::OnButton2()
{
::EnumChildWindows(::GetForegroundWindow(),EnumChildProc,NULL);
}

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

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
if ( GetParent(hwnd)==NULL  &&  IsWindowVisible(hwnd) )
{
  TCHAR WindowText[100]={0};
  ::SendMessage(hwnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  trace(WindowText);
}
return true;  
}
 
void CDemoDlg::OnButton3()
{
EnumWindows(EnumWindowsProc ,(LPARAM)0 );
}

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

//简单的寻找子窗口句柄
HWND hwnd=::FindWindowEx(m_hWnd,NULL,"#32770",NULL);

2.枚举窗口的应用

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

(1)单一函数的形式

/****************************************************************************
寻找指定类名的窗口句柄 方法一
****************************************************************************/
HWND FindWithClassName(HWND ParentWnd,TCHAR* FindClassName)
{
HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);
for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
{
  TCHAR ClassName[100]={0};
  ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));
  if (_tcscmp(ClassName,FindClassName)==0)
   return hChild;
 
  HWND FindWnd=FindWithClassName(hChild,FindClassName);
  if (FindWnd)
   return FindWnd;
}
return NULL;
}
//调用
void CDemoDlg::OnButton6()
{
HWND hWnd=FindWithClassName(::GetForegroundWindow(),"Edit");
if (hWnd)
{
  TCHAR WindowText[100]={0};
  ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  trace(WindowText);
}
else
{
  trace("can not find");
}
}

(2)类的形式

/****************************************************************************
寻找指定类名的窗口句柄 方法二
****************************************************************************/
class CFindWithClassName
{
public:
static HWND FindWnd;
static TCHAR ClassName[100];
static BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)  
{
  ::GetClassName(hwnd,ClassName,sizeof(ClassName)/sizeof(TCHAR)); 
  if (_tcscmp(ClassName,(TCHAR*)lParam)==0)
  {
   FindWnd=hwnd;
   return false;
  } 
  return TRUE ; 
}
static HWND Find(HWND hwndParent, TCHAR* FindClassName)
{
  ::EnumChildWindows(hwndParent,EnumChildProc,(LPARAM)FindClassName);
  return FindWnd;
}
};
HWND CFindWithClassName::FindWnd=NULL;
TCHAR CFindWithClassName::ClassName[100]=_T("");
//调用
void CDemoDlg::OnButton7()
{
HWND hWnd=CFindWithClassName::Find(::GetForegroundWindow(),_T("Edit"));
if (hWnd)
{
  TCHAR WindowText[100]={0};
  ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  trace(WindowText);
}
else
{
  trace("can not find");
}
}

(3)大师Paul DiLascia写的。

////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
//  CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
//////////////////
// This private function is used with EnumChildWindows to find the child
// with a given class name. Returns FALSE if found (to stop enumerating).
//
static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
  CFindWnd *pfw = (CFindWnd*)lParam;
  HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
  if (hwnd) {
   pfw->m_hWnd = hwnd; // found: save it
   return FALSE;   // stop enumerating
  }
  EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
  return TRUE;    // keep looking
}
public:
LPCSTR m_classname;   // class name to look for
HWND m_hWnd;     // HWND if found
// ctor does the work--just instantiate and go
CFindWnd(HWND hwndParent, LPCSTR classname)
  : m_hWnd(NULL), m_classname(classname)
{
  FindChildClassHwnd(hwndParent, (LPARAM)this);
}
};
void CDemoDlg::OnButton5()
{
CFindWnd fw(::GetForegroundWindow(),"Edit");
TCHAR WindowText[30]={0};
::SendMessage(fw.m_hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
trace(WindowText);
}

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

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

(1)方法一,类的形式

class CFindWithID
{
public:
static BOOL CALLBACK _EnumChildProc( HWND hwnd, LPARAM lParam ) 

  //*(DWORD*)lParam 即是传进来的Control ID
  if ( GetDlgCtrlID(hwnd) == *(DWORD*)lParam  )//判断是否为需要的控件 
  { 
   *(HWND*)lParam=hwnd; //强制转换
   return FALSE; 
  } 
  return TRUE; 

 
static HWND Find(HWND TopWnd, DWORD ControlID ) 

  EnumChildWindows( TopWnd , _EnumChildProc, (LPARAM)&ControlID ); 
  return ::IsWindow( (HWND)ControlID ) ? (HWND)ControlID : NULL;
}
};
void CDemoDlg::OnButton8()
{
HWND hWnd=CFindWithID::Find(::GetForegroundWindow(),0x000003e9);
if (hWnd)
{
  TCHAR WindowText[100]={0};
  ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  trace(WindowText);
}
else
{
  trace("can not find");
}
}

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

HWND FindControlWnd(HWND ParentWnd,DWORD ControlID)
{
HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);
for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
{
  //判断是否为需要的控件 
  if ( GetDlgCtrlID(hChild) == ControlID  )
   return hChild;
  HWND FindWnd=FindControlWnd(hChild,ControlID);
  if (FindWnd)
   return FindWnd;
}
return NULL;
}
void CDemoDlg::OnButton9()
{
HWND hWnd=FindControlWnd(::GetForegroundWindow(),0x000003e9);
if (hWnd)
{
  TCHAR WindowText[100]={0};
  ::SendMessage(hWnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText);
  trace(WindowText);
}
else
{
  trace("can not find");

}

给我留言

留言无头像?