C++ 窗口扫描器

标签 c++

需要循环方面的帮助。我的想法是扫描窗口,当找到一个窗口时,检查它的进程 ID,所以如果已经找到该窗口,就不要尝试再次找到它。我的代码中有一段是这样的:

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;
            }
        }
    }
}

希望大家帮帮我。干杯

编辑:新代码

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;
}

这部分*((int *)lParam)++;代码不起作用。错误是:表达式必须是可修改的左值@MikeKwan

编辑:我又遇到了同样的问题@MikeKwan :)

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

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

我每次都使用这段代码来检测窗口,但它检测到一个窗口并一次又一次地重新检测同一个窗口,所以没有任何改变:/

最佳答案

在您的情况下,FindWindow 不知道您已经看到的窗口列表,因此它将继续返回相同的窗口(尽管这是实现定义的)。无论如何,FindWindow 无法执行您正在尝试的操作(在搜索期间跳过窗口)。

如果你只想枚举所有窗口一次,你应该使用EnumWindows .我为 EnumWindows 编写了一些示例 C 代码,它可以实现我猜你想要实现的目标。您需要将其转换为 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;
}

(刚写在答题窗口,可能会有小错误)。

关于C++ 窗口扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855446/

相关文章:

c++ - 每当成员变量可以由可变参数构造时,有条件地启用构造函数

c++ - dll 在 VS2010 项目中运行良好,但在 VS6 应用程序中出现访问冲突

c++ - 堆栈内存未释放

c++ - 新对象中的空函数指针实际上不是 nullptr

c++ - 显式加载库的干净方法

c++ - 如何在 Mac 上启用 C++17?

c++ - 使用 clang v3.4 在 ubuntu_x86-64 上为 arm 编译一个 c/c++ 源文件

c++ - C++ 中随机位置的二进制输出到文件

c++ - 优化 YUY2 到 I420 的转换

c++ - 我不断收到 'no match for call to' 错误