2017-04-18 64 views
1

因此,我正在構建一個程序,該程序根據用戶定義的進程是否正在運行來有條件地複製一些文件。現在,由於用戶正在選擇流程,因此我想過濾他們可以選擇的流程到他們真正關心的流程。基本上我只想要可以被alt-tabbed的進程/窗口。我發現了一些獲取正在運行的進程的方法,但我沒有多少運氣來弄清楚如何將它們過濾到我正在尋找的目標。現在,這是我使用來獲取進程名&窗口名稱的代碼,但我覺得可能有一個更好的方式來做到這一點:獲取用戶相關進程/窗口列表

(僅供參考,我使用的這個QT,所以這是其中的QString和QStringLists來自何方)

HWND hwnd_current = GetWindow(GetDesktopWindow(), GW_CHILD); 
QStringList process_list; 
do { 
    wchar_t str_window_name[MAX_PATH]; 
    DWORD pid; 
    DWORD exStyles = (DWORD)GetWindowLongPtr(hwnd_current, GWL_EXSTYLE); 
    DWORD styles = (DWORD)GetWindowLongPtr(hwnd_current, GWL_STYLE); 

    if(!((exStyles & WS_EX_TOOLWINDOW) == 0 && (styles & WS_CHILD) == 0)){ 
     continue; 
    } 
    if(!GetWindowText(hwnd_current, str_window_name, MAX_PATH)){ 
     continue; 
    } 
    GetWindowThreadProcessId(hwnd_current, &pid); 
    if(pid == GetCurrentProcessId()){ 
     continue; 
    } 

    wchar_t fileName[MAX_PATH]; 
    LPWSTR file_name; 
    HANDLE hProcess; 
    hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, FALSE, pid); 
    if(hProcess){ 
     DWORD dwSize = MAX_PATH; 
     QueryFullProcessImageName(hProcess, 0, fileName, &dwSize); 
     file_name = PathFindFileName(fileName); 
    } 
    CloseHandle(hProcess); 
    QString boxString = QString("["); 
    #ifdef UNICODE 
    QString q_file_name = QString::fromStdWString(file_name); 
    QString q_str_window_name = QString::fromStdWString(str_window_name); 
    #else 
    QString q_file_name = QString::fromStdString(file_name); 
    QString q_str_window_name = QString::fromStdString(str_window_name); 
    #endif 
    boxString.append(q_file_name); 
    boxString.append("] "); 
    boxString.append(q_str_window_name); 

    if(!q_file_name.isEmpty() && !q_str_window_name.isEmpty() && !pNameList.contains(boxString) && !process_list.contains(q_file_name)){ 
     if(!q_str_window_name.endsWith("MSCTFIME UI") && !q_str_window_name.endsWith("Default IME")){ 
      process_list.append(q_file_name); 
      pNameList.append(boxString); 
     } 
    } 

} while (hwnd_current = GetNextWindow(hwnd_current, GW_HWNDNEXT)); 

任何意見將非常感謝!謝謝!

回答