2016-09-20 98 views
-2

我在Visual C++ 6.0的C++中使用以下程序,以在MS Paint程序打開時通過消息框通知我。它使用MS Paint的WINDOW的確切名稱,它是「無標題 - 畫圖」。然而,現在我需要讓程序告訴我消息框,當我知道實際WINDOW的名稱的一部分,例如,如果窗口是「Abcdefgh - Paint」,並且我以這種方式設置了字符串名 - std :: wstring windowName(L「Paint」); - 該計劃再次工作。使用下面的3行代碼的程序工作正常時,實際窗口名稱是MS畫圖窗口的確切名稱:使用WINDOW部分獲取processID標題

HWND windowHandle = FindWindowW(NULL, windowName.c_str()); 
DWORD* processID = new DWORD; 
GetWindowThreadProcessId(windowHandle, processID); 

但如果字符串的windowName僅僅是名稱的一部分,它不會工作,我意思是如果它是「繪畫」。 有人可以告訴我如何做到這一點?我想要列出所有打開的WINDOWS的名稱,並將它們與真實名稱的一部分進行比較,我的意思是在其名稱中搜索子字符串「Paint」的匹配,但我不知道如何獲取所有打開窗戶。此外,這是非常重要的,我的電腦是舊的,我正在使用Visual C++ 6.0,所以我不能使用C++和程序環境的所有進化功能時下,我的意思是,我不能使用編譯正確的代碼.NET,但不在Visual C++ 6.0中編譯。 感謝

#include "stdafx.h" 
#include < iostream> 
#include < string> 
#include < windows.h> 
#include < sstream> 
#include < ctime> 

int APIENTRY WinMain(HINSTANCE hInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR  lpCmdLine, 
       int  nCmdShow) 
{ 
// TODO: Place code here. 
std::wstring windowName(L"Untitled - Paint"); 




while(true) 
{ 

    Sleep(1000*5); 

time_t t = time(0); // get time now 
struct tm * now = localtime(& t); 
int tday = now->tm_mday; 
int tmin = now->tm_min; 
int thour = now->tm_hour; 


HWND windowHandle = FindWindowW(NULL, windowName.c_str()); 
DWORD* processID = new DWORD; 
GetWindowThreadProcessId(windowHandle, processID); 

char probaintstr[20]; 
sprintf(probaintstr,"%d",*processID); 





if(strlen(probaintstr) <=5) 
{ 

Sleep(1000*10); 

MessageBox(NULL,"niama go Notepad ili Wordpad","zaglavie",MB_OK); 
} 
else { 

} 




} 


return 0; 
} 
+0

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx [的Win32的FindWindow函數的 – sigy

+0

可能的複製( )可以找到一個確切的標題,但「try.bat - 記事本」?](http://stackoverflow.com/questions/3327666/win32s-findwindow-can-find-a-particular-window-與確切的標題但什麼) –

+0

不要通過爲每個迭代的'processID'分配一個'新的DWORD'來泄漏內存。把它作爲局部變量'DWORD processID;',然後使用'&'操作符來獲得一個指針:'GetWindowThreadProcessId(windowHandle,&processID);'然後你可以在不需要解引用的情況下使用它(比如'DWORD'值,而不使用'*')。在你的例子中這不是一個大問題,因爲'DWORD'很小,迭代週期很長,但你不想養成創建泄漏應用程序的習慣。 –

回答

0

您可以使用EnumWindows,例如

BOOL CALLBACK enumWindow(HWND hwnd, LPARAM lp) 
{ 
    std::string str(512, 0); 
    int len = GetWindowText(hwnd, &str[0], str.size()); 
    if (str.find("Paint") != std::string::npos) 
    { 
     MessageBox(0, str.c_str(), 0, 0); 
    } 
    return true; 
} 

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
{ 
    EnumWindows(enumWindow, 0); 
    return 0; 
} 

或者你可以使用FindWindowEx,尋找類名。 MS Paint的類名是"MSPaintApp"。您可以從Windows的「間諜」實用程序中找到此信息。

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
{ 
    for (HWND hwnd = NULL;;) 
    { 
     hwnd = FindWindowExA(NULL, hwnd, "MSPaintApp", 0); 
     if (!hwnd) 
      break; 

     std::string str(512, 0); 
     int len = GetWindowText(hwnd, &str[0], 512); 
     str.resize(len); 
     if (str.find("Paint") != std::string::npos) 
      MessageBox(0, str.c_str(), 0, 0); 
    } 
    return 0; 
} 

對於進程ID,您不需要分配內存。只需使用一個參考:

DWORD processID; 
GetWindowThreadProcessId(windowHandle, &processID);