简体   繁体   English

替换“睡眠”,直到打开桌面上的窗口

[英]Replace Sleep until Window on desktop opened

When, I open some of the software applications I have to wait 2-3 seconds until window will show on desktop. 当我打开某些软件应用程序时,我必须等待2-3秒,直到窗口在桌面上显示。 I have to use Sleep(2000); 我必须使用Sleep(2000); and then call method set always on top. 然后将调用方法始终设置在最前面。 I'm trying to replace Sleep in my code. 我正在尝试在代码中替换Sleep。 I would like to get signal from opened window and after this, call a method, which allows opened window be always on top. 我想从打开的窗口中获取信号,然后,调用一个方法,该方法允许打开的窗口始终位于顶部。 Here's my code: 这是我的代码:

BOOL CALLBACK EnumWindowsProc(HWND windowHandle, LPARAM lParam)
{

    DWORD searchedProcessId = (DWORD)lParam;
    DWORD windowProcessId = 0;
    GetWindowThreadProcessId(windowHandle, &windowProcessId);
    cout << "Process id: " << windowProcessId << endl;


    if(searchedProcessId == windowProcessId) {
        HWND hwnd = windowHandle;

        Sleep(2000);                    
        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
        cout << "Process ID found!" << endl;

        return TRUE;
    }
    return TRUE;
}

void AlwaysOnTop(int processId)
{
        EnumWindows(&EnumWindowsProc, (LPARAM)processId);   
}


void AlwaysOnTop(char *name)
{

        cout << "String: " << name << endl;

        Sleep(2000);
        HWND h = FindWindow(NULL, (LPCSTR) name);
        SetActiveWindow(h); 
        SetForegroundWindow(h);
        SetWindowPos(h, HWND_TOPMOST, 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);

}



int main()
{

    char s[] = {"Application"};

    AlwaysOnTop(s);
    //AlwaysOnTop(2307);

    system("PAUSE");
    return 0;
}

Probably the best you can do is to call WaitForInputIdle : 可能最好的办法是调用WaitForInputIdle

Waits until the specified process has finished processing its initial input and is waiting for user input with no input pending, or until the time-out interval has elapsed. 等待直到指定的进程完成其初始输入的处理并等待没有输入挂起的用户输入,或者直到超时间隔过去。

This is the closest you can get to a general way to wait until a process is showing its UI. 这是您可以与一般方法最接近的方法,可以等到进程显示其UI为止。 It won't always do what you want, but it's the best there is. 它不会总是做您想要的,但这是最好的。

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

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