2012-07-10 24 views

回答

2

嘗試使用此GetWindow API遍歷顯示的窗口句柄。使用GetWindowText API獲取標題標題(如果有)。

HWND wnd = ::GetWindow(this->GetSafeHWND(), GW_HWNDFIRST); 
if(!wnd) 
{ 
    return; 
} 
CString csWindows = ""; 
do 
{ 
    CString csText; 
    ::GetWindowText(wnd, csText.GetBuffer(MAX_PATH), MAX_PATH); 
    csText.ReleaseBuffer(); 
    if(!csText.IsEmpty()) 
    { 
     csWindows += csText + "\n"; 
    } 
}while(wnd = ::GetWindow(wnd, GW_HWNDNEXT)); 
AfxMessageBox(csWindows); 

上面的代碼應該工作。如果您只想看到可見的窗口,請使用::IsWindowVisible API來檢查它。

更新:

調用GetWindow來執行此任務的應用程序有可能被 陷入無限循環或引用的句柄 已銷燬的窗口。將EnumWindows用於頂級Windows和 EnumChildWindows用於Child Windows或EnumThreadWindows用於所有 與子線程關聯的非子窗口是首選方法。

Microsoft Support

+0

非常感謝.... 希望這個代碼也工作 BOOL CALLBACK EnumWindowsProc(HWND的HWND,長的lParam){ 炭的buff [255]引;如果(IsWindowVisible(hWnd))GetWindowText(hWnd,(LPWSTR)buff,254); } return TRUE; (argc,argv); } int main(int argc,char * argv []) { QCoreApplication a(argc,argv); EnumWindows(EnumWindowsProc,0); return 0; } – Sudix 2012-07-10 09:21:10

+0

EnumWindows枚舉顯示的所有頂級窗口。它應該工作,如果這是你想要的。 – 2012-07-10 09:35:02

相關問題