第一种方法: 1.先获得桌面窗口 CWnd* pDesktopWnd = CWnd::GetDesktopWindow(); 2.获得一个子窗口 CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD); 3.循环取得桌面下的所有子窗口 while(pWnd != NULL) { //获得窗口类名 CString strClassName = _T("");//应该用TCHAR,用CStrting没有测试通过. ::GetClassName(pWnd->GetSafeHwnd(),strClassName.GetBuffer(256),256); //获得窗口标题 CString strWindowText = _T(""); ::GetWindowText(pWnd->GetSafeHwnd(),strWindowText.GetBuffer(256),256); //继续下一个子窗口 pWnd = pWnd->GetWindow(GW_HWNDNEXT); } 第二种方法: 1.定义存放窗口句柄变量,和下标计数器 HWND m_hWndFind[1000]; int m_Index; 2.先写一个BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam) 的回调函数. BOOL CAllwindowsDlg::EnumWindowsProc(HWND hWnd, LPARAM lParam) { //查找可见的窗口 if(::GetWindowLong(hWnd,GWL_STYLE)& WS_VISIBLE) { m_hwndFind[m_Index] = hWnd;//record the HWND handle into array m_Index++;//count start } return 1; } 3.调用(这个回调函数回自动递归的便利所有可见窗口,直到完毕) ::EnumWindows(CAllwindowsDlg::EnumWindowsProc,NULL); 4.取得窗口名称和类名 for(int i = 0;i <=m_Index;i++) { HWND m_wnd = m_hwndFind[i]; ::GetWindowText(m_wnd,m_store,128); ::GetClassName(m_wnd,m_strClass,MAX_PATH-1); //获得窗口类名 CString strClassName = _T(""); ::GetClassName(m_wnd,strClassName.GetBuffer(256),256); //获得窗口标题 CString strWindowText = _T(""); ::GetWindowText(m_wnd,strWindowText.GetBuffer(256),256); }
|
|