FindWindow does not find the a window

后端 未结 4 1127
走了就别回头了
走了就别回头了 2020-12-09 21:25

I have a plan for make a simple trainer console with C++ but first step I\'ve got problem with FindWindow()

#include 
#include 
         


        
相关标签:
4条回答
  • 2020-12-09 21:27

    According to MSDN

    lpWindowName [in, optional]

    Type: LPCTSTR
    
    The window name (the window's title). If this parameter is NULL, all window names match.
    

    Thus your WindowName can't be "Mozilla Firefox", because the Firefox window's title is never "Mozilla Firefox" but it could be "Mozilla Firefox Start Page - Mozilla Firefox" or something depends on the web page's name. Here is the example picture

    Thus your code should be like this, (the code below only work - only work if you have the exact window's title name: "Mozilla Firefox Start Page - Mozilla Firefox" like the image above. I have tested on Windows 8.1 and it worked)

    void CaptureWindow()
    {
    
    
    RECT rc;
    HWND hwnd = ::FindWindow(0, _T("Mozilla Firefox Start Page - Mozilla Firefox"));//::FindWindow(0,_T("ScreenCapture (Running) - Microsoft Visual Studio"));//::FindWindow(0, _T("Calculator"));//= FindWindow("Notepad", NULL);    //You get the ideal?
    if (hwnd == NULL)
    {
        return;
    }
    GetClientRect(hwnd, &rc);
    
    //create
    HDC hdcScreen = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcScreen);
    HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
        rc.right - rc.left, rc.bottom - rc.top);
    SelectObject(hdc, hbmp);
    
    //Print to memory hdc
    PrintWindow(hwnd, hdc, PW_CLIENTONLY);
    
    //copy to clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();
    
    //release
    DeleteDC(hdc);
    DeleteObject(hbmp);
    ReleaseDC(NULL, hdcScreen);
    
    //Play(TEXT("photoclick.wav"));//This is just a function to play a sound, you can write it yourself, but it doesn't matter in this example so I comment it out.
    }
    
    0 讨论(0)
  • 2020-12-09 21:28
     HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
    
    0 讨论(0)
  • 2020-12-09 21:36

    FindWindow only finds the window if it has the exact specified title, not just a substring.

    Alternatively you can:


    search for the window class name:

    HWND hWnd = FindWindow("MozillaWindowClass", 0);
    

    enumerate all windows and perform custom pattern searches on the titles:

    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
        char buffer[128];
        int written = GetWindowTextA(hwnd, buffer, 128);
        if (written && strstr(buffer,"Mozilla Firefox") != NULL) {
            *(HWND*)lParam = hwnd;
            return FALSE;
        }
        return TRUE;
    }
    
    HWND GetFirefoxHwnd()
    {
        HWND hWnd = NULL;
        EnumWindows(EnumWindowsProc, &hWnd);
        return hWnd;
    }
    
    0 讨论(0)
  • 2020-12-09 21:48

    You need to use the full name of the application (as seen in Windows Task Manager -> Application tab)

    Example:

    Google - Mozilla Firefox

    (after opening a Google tab in Firefox)

    0 讨论(0)
提交回复
热议问题