我需要获取当前的前景窗口句柄并将其与保存的先前窗口句柄进行比较,以查看它们是否匹配,但是我无法使用前景窗口标题,因为标题经常更改

这是我用来比较标题的代码

char cWindow[MAX_PATH];
char nWindow[MAX_PATH];
GetWindowTextA(GetForegroundWindow(), cWindow, sizeof(cWindow));


//Later in code
GetWindowTextA(GetForegroundWindow(), cWindow, sizeof(cWindow));
if (strcmp(nWindow, cWindow) != 0)
{
    fputs("found!",file);
    strcpy(nWindow, cWindow);
}

最佳答案

当您要检查前景窗口是否已更改时,应直接比较窗口句柄,而不包含它们的标题。

HWND oldForegroundWindow = GetForegroundWindow();

HWND newForegroundWindow = GetForegrundWindow();
if (newForegroundWindow != oldForegroundWindow) {
  ForegroundWindowHasChanged(oldForegroundWindow, nForegroundWindow);
  oldForegroundWindow = newForeroundWindow;
}

08-26 00:02