获取当前全部打开窗口两种方法--上位机vc
获取当前全部打开窗口两种方法--上位机vc
遍历全部已打开窗口是很常用的功能,当前例程用两种方法实现当前全部打开窗口的获取功能
拖拽一列表控件ID保持默认IDC_LIST1,再添加两按钮,关联点击 函数,实现两种功能;
- void CGkbc8Dlg::OnButton1()
- {
- CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
- 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);
- }
- 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;
- }
- void CGkbc8Dlg::OnButton2()
- {
- CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
- pList->DeleteAllItems();
- pList->SetRedraw(FALSE);
- ::EnumWindows(EnumWndProc, (LPARAM)pList);//枚举窗口
- pList->SetRedraw(TRUE);
- }
复制代码
记得列表控件在程序 初始化时添加表头及一些属性的设置
- CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
- pList->ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);
- pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
- pList->InsertColumn(0, _T("窗口类名"), LVCFMT_LEFT, 150);
- pList->InsertColumn(1, _T("窗口标题"), LVCFMT_LEFT, 150);
复制代码
|