获得桌面所有窗口句柄的方法总结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“获取窗口句柄的工具”。
获得桌面所有窗口句柄的方法总结
第一种方法:
1.先获得桌面窗口
CWnd* pDesktopWnd = CWnd::GetDesktopWindow();2.获得一个子窗口
CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);3.循环取得桌面下的所有子窗口
while(pWnd!= NULL){
//获得窗口类名
CString strClaName = _T(“”);//应该用TCHAR,用CStrting没有测试通过.::GetClaName(pWnd->GetSafeHwnd(),strClaName.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
{
HWND m_wnd = m_hwndFind[i];
::GetWindowText(m_wnd,m_store,128);
::GetClaName(m_wnd,m_strCla,MAX_PATH-1);
//获得窗口类名
CString strClaName = _T(“”);
::GetClaName(m_wnd,strClaName.GetBuffer(256),256);
//获得窗口标题
CString strWindowText = _T(“”);
::GetWindowText(m_wnd,strWindowText.GetBuffer(256),256);
}