2011-10-31 109 views
0

如何檢查特定窗口是否打開。我只有部分窗口的名字。我想在QT控制檯應用程序中使用EnumWindows(),但我得到一些錯誤,指出「main.obj:-1:錯誤:無法解析的外部符號imp__GetWindowTextW @ 12在函數中引用」int __stdcall EnumWindowsProc(struct HWND *,long) 「(?EnumWindowsProc @@ YGHPAUHWND __ @@Ĵ@ Z)」Qt - 獲取所有打開的窗口標題

下面是我的示例代碼

BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) { 
    char buff[255]; 

    if (IsWindowVisible(hWnd)) { 
     GetWindowText(hWnd, (LPWSTR) buff, 254); 
    } 
    return TRUE; 
} 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    EnumWindows(EnumWindowsProc, 0); 

    return 0; 
} 
+1

我在這裏做了一些假設,但你鏈接到user32.lib? – Bart

+0

簽出[Qt + win32 + mingw原生Windows API鏈接問題](http://stackoverflow.com/questions/267672) –

+0

我設法編譯我的QT應用程序沒有錯誤,加入#include Lynnooi

回答

0

EnumWindowsProc不是來自Qt的,它是一個Windows API函數,你需要包括WINDOWS.H

我沒有使用Qt,代碼可以通過complie,但輸出似乎不正確。總之,這裏是我的代碼

#include <conio.h> 
#include <iostream> 
#include <windows.h> 
using namespace std; 
char buff[255]; 
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) 
{ 
    if (IsWindowVisible(hWnd)) 
    { 
     GetWindowText(hWnd, (LPWSTR) buff, 254); 
    } 
    return TRUE; 
} 


int main() 
{ 
    EnumWindows(EnumWindowsProc, 0); 
    for(int i = 0; i != 254; ++i) 
     cout << buff[i]; 
    getch(); 
    return 0; 
} 
+0

我已經包括在我的代碼中的windows.h – Lynnooi

+0

@Lynnooi你的代碼在我的應用程序中工作...嘗試不使用Qt,使用本機C++代碼。 – shengy

0

您可以使用

Application.OpenForms["FormName"]

檢查形式是開放與否

+0

我想獲得所有打開的應用程序窗口,包括那些不是QT應用程序的窗口 – Lynnooi

2

這是一個鏈接錯誤,而不是編譯錯誤。

您已正確包含windows.h,但您還需要將導入庫添加到鏈接器選項。示例代碼中的所有三個Win32函數都需要鏈接user32.lib