这进而分享两种方法
void CDemoDlg::OnTest1()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->DeleteAllItems();
pList->SetRedraw(FALSE);
//获得桌面窗口
CWnd* pDesktopWnd = CWnd::GetDesktopWindow();
//获得第一个子窗口
CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);
while (pWnd != NULL)
{
int nItem = pList->GetItemCount();
//获得窗口类名
CString strClassName = _T("");
::GetClassName(pWnd->GetSafeHwnd(), strClassName.GetBuffer(256), 256);
strClassName.ReleaseBuffer();
pList->InsertItem(nItem, strClassName);
//获得窗口标题
CString strWindowText= _T("");
::GetWindowText(pWnd->GetSafeHwnd(), strWindowText.GetBuffer(256), 256);
strWindowText.ReleaseBuffer();
pList->SetItemText(nItem, 1, strWindowText);
//继续下一个子窗口
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
pList->SetRedraw(TRUE);
}
方法2
BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
if (hwnd == NULL)
{
return FALSE;
}
CListCtrl* pList = (CListCtrl*)lParam;
int nItem = pList->GetItemCount();
//获得窗口类名
CString strClassName = _T("");
::GetClassName(hwnd, strClassName.GetBuffer(256), 256);
strClassName.ReleaseBuffer();
pList->InsertItem(nItem, strClassName);
//获得窗口标题
CString strWindowText= _T("");
::GetWindowText(hwnd, strWindowText.GetBuffer(256), 256);
strWindowText.ReleaseBuffer();
pList->SetItemText(nItem, 1, strWindowText);
return TRUE;
}
上面出现的IDC_LIST为列表控件ID.
用于显示结果。
可以事先添加
列表控件要事先初始化添加一些表头
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);
pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
pList->InsertColumn(0, _T("窗口类名"), LVCFMT_LEFT, 100);
pList->InsertColumn(1, _T("窗口标题"), LVCFMT_LEFT, 100);
也可以下载源代码学习
|