简体   繁体   English

C ++窗口扫描器

[英]C++ Window Scanner

Need help with the loop. 需要帮助的循环。 My Idea was scann for windows and when a window found check its procces Id so if the window was already found dont try to find it again. 已扫描“我的想法”中的窗口,当找到一个窗口时,请检查其过程ID,因此如果已经找到该窗口,请不要尝试再次找到它。 A piece from my code is like that: 我的代码片段是这样的:

if (Selection == 1)
{
    cout << endl << "================================= Scan Result =================================" << endl;
    cout << endl << "Scan Log:                                                        Times Found: " << TimesFound << endl;
    cout << "   - Scanning windows..." << endl;

    while(PressedKey != 27)
    {
        if (kbhit())
        {
            PressedKey = getch();
        }

        HWND WindowFound = FindWindow(0, "Untitled - Notepad");

        if (WindowFound != 0) 
        {
                            // My Idea was compare the procces Id of the found window
                            // to learn the found window was already found
            DWORD ProcessId;
            GetWindowThreadProcessId(WindowFound, &ProcessId);

            if(ProcessId != OtherId)
            {
                TimesFound++;
                cout << "Window found ! Times found: " << TimesFound << endl;
            }
        }
    }
}

I hope you guys can help me. 我希望你们能帮助我。 Cheers 干杯

Edit: New Code 编辑:新代码

BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
    wchar_t lpString[32];

    GetWindowText(hwnd, (LPSTR)lpString, _countof(lpString));

    if(wcscmp(lpString, L"Untitled - Notepad") == 0) 
    {
        *((int *)lParam)++;
    }

    return TRUE;
}

This part of *((int *)lParam)++; *((int *)lParam)++的这一部分; code not works. 代码不起作用。 Error is: expression must be a modifiable lvalue @MikeKwan 错误是:表达式必须是可修改的左值@MikeKwan

Edit: Me with same question again @MikeKwan :) 编辑:我再次遇到相同的问题@MikeKwan :)

    while(PressedKey != 27)
    {
        if (kbhit())
        {
            PressedKey = getch();
            printf("Times found: %d \n", TimesFound);
        }

        EnumWindows(EnumFunc, (LPARAM)&TimesFound);
    }

I use this code to detect window every time but It detects a window and it redetects the same window again and again so nothing is changed :/ 我每次都使用此代码检测窗口,但是它检测到一个窗口,并且一次又一次地检测到同一窗口,因此什么都没有改变:/

In your case, FindWindow is not aware of your list of already seen windows so it will continue to return the same window (although this is implementation defined). 在您的情况下, FindWindow无法FindWindow您已经看到的窗口列表,因此它将继续返回同一窗口(尽管这是实现定义的)。 Regardless, FindWindow does not have the capability to do what you are attempting (skip windows during its search). 无论如何, FindWindow无法执行您要尝试的功能(在搜索过程中跳过窗口)。

If you want to enumerate all windows only once, you should use EnumWindows . 如果只想枚举所有窗口一次,则应使用EnumWindows I wrote some example C code for EnumWindows that does what I am guessing you want to achieve. 我为EnumWindows编写了一些示例C代码,该代码可以实现我想达到的目标。 You will need to convert it to C++ but your usage of C++ is not particularly of the language at the moment anyway. 您将需要将其转换为C ++,但目前对C ++的使用并不特别。

BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
    /*
     * We don't care about getting the whole string,
     * just enough to do the comparison. GetWindowText
     * will truncate the string if we tell it to.
     */
    wchar_t lpString[32];

    GetWindowText(hwnd, lpString, _countof);

    if(wcscmp(lpString, L"Untitled - Notepad") == 0) {
        (*(int *)param)++;
    }

    return TRUE;
}

int main(void)
{
    int numFound = 0;

    EnumWindows(EnumFunc, (LPARAM)&numFound);
    return ERROR_SUCCESS;
}

(Just wrote this in the answer window so there might be small errors). (只需将其写在答案窗口中,这样可能会有一些小错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM