简体   繁体   English

比较当前前景窗口句柄

[英]Comparing current foreground window handle

I need to get the current foreground window handle and compare it to a saved previous window handle to see if they match, but I can't use foreground window title, because the title changes often 我需要获取当前的前景窗口句柄并将其与保存的先前窗口句柄进行比较,以查看它们是否匹配,但是我不能使用前景窗口标题,因为标题经常更改

This is the code I'm using to compare the titles 这是我用来比较标题的代码

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

When you want to check whether the foreground window has changed, you should compare the window handles directly, without their titles. 当您要检查前景窗口是否已更改时,应直接比较窗口句柄 ,而不包含它们的标题。

HWND oldForegroundWindow = GetForegroundWindow();

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

Errr you are NOT setting nWindow to anything both your calls are setting cWindow. 错误的是你没有将nWindow设置为你的呼叫设置cWindow的任何东西。 Do you think that may be an issue? 您认为这可能是个问题吗?

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

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