当我打开一些软件应用程序时,我必须等待 2-3 秒,直到窗口显示在桌面上。我必须使用 Sleep(2000);然后调用方法 set always on top。我正在尝试在我的代码中替换 Sleep。我想从打开的窗口获取信号,然后调用一个方法,使打开的窗口始终位于最前面。 这是我的代码:
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;
}
最佳答案
可能最好的办法就是调用 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.
这是最接近等待进程显示其 UI 的一般方法。它不会总能满足您的要求,但它是目前最好的。
关于c++ - 替换 sleep 直到桌面上的窗口打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480605/