为什么我的Windows消息框总是选择您的c ++路径?

问题描述 投票:-3回答:1

以下是我要附加到窗口的程序,它可以工作,但是我希望用户可以选择尝试附加窗口而不重新打开程序。我使用switch语句尝试了此操作,但是如果未找到该进程而用户打开了该进程,则按重试,为什么在该进程未打开时会重复该错误。代码-

class attach 
{
DWORD ProcID;
HWND hwnd = FindWindowA(NULL, "SONIC HEROES(TM)");
void attempt()
{
    if (hwnd == NULL)
    {
        const int Window = MessageBoxA(0, "Failed to attach Window!", "Attention", MB_RETRYCANCEL | MB_ICONERROR);
        switch (Window)
        {
        case IDRETRY:
            attempt();
            break;
        case IDCANCEL:
            exit(-1);
            break;
        }
    }
    else
    {
        MessageBoxA(0, "Window Found!", "Attention", MB_OK | MB_ICONHAND);
    }
}
};
c++ winapi
1个回答
0
投票

您可以将FindWindowA放在attempt()函数中,并创建一个要完成的数组。

这里是一个最小的代码示例:

#include <Windows.h>
#include <iostream>

using namespace std;

CHAR* name = (CHAR*)malloc(1024);

class attach
{
public:     
    void attempt();

private:
    DWORD ProcID;
    HWND hwnd;
};

void attach::attempt()
{      
    hwnd = FindWindowA(NULL, name);
    if (hwnd == NULL)
    {
        hwnd = FindWindowA(NULL, name);
        const int Window = MessageBoxA(0, "Failed to attach Window!", "Attention", MB_RETRYCANCEL | MB_ICONERROR);
        switch (Window)
        {
        case IDRETRY:
            attempt();
            break;
        case IDCANCEL:
            return;
        }
    }
    else
    {
        MessageBoxA(0, "Window Found!", "Attention", MB_OK | MB_ICONHAND);
        cout << "If you want to attach new process, please input new application name:" << endl;
        cin >> name;
        attempt();
    }
}

int main()
{
    cout << "Input your application name:" << endl;
    cin >> name;
    attach a;
    a.attempt();

    free(name);

    return 0;

}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.