[英]C++ Window Scanner
需要帮助的循环。 已扫描“我的想法”中的窗口,当找到一个窗口时,请检查其过程ID,因此如果已经找到该窗口,请不要尝试再次找到它。 我的代码片段是这样的:
if (Selection == 1)
{
cout << endl << "================================= Scan Result =================================" << endl;
cout << endl << "Scan Log: Times Found: " << TimesFound << endl;
cout << " - Scanning windows..." << endl;
while(PressedKey != 27)
{
if (kbhit())
{
PressedKey = getch();
}
HWND WindowFound = FindWindow(0, "Untitled - Notepad");
if (WindowFound != 0)
{
// My Idea was compare the procces Id of the found window
// to learn the found window was already found
DWORD ProcessId;
GetWindowThreadProcessId(WindowFound, &ProcessId);
if(ProcessId != OtherId)
{
TimesFound++;
cout << "Window found ! Times found: " << TimesFound << endl;
}
}
}
}
我希望你们能帮助我。 干杯
编辑:新代码
BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
wchar_t lpString[32];
GetWindowText(hwnd, (LPSTR)lpString, _countof(lpString));
if(wcscmp(lpString, L"Untitled - Notepad") == 0)
{
*((int *)lParam)++;
}
return TRUE;
}
*((int *)lParam)++的这一部分; 代码不起作用。 错误是:表达式必须是可修改的左值@MikeKwan
编辑:我再次遇到相同的问题@MikeKwan :)
while(PressedKey != 27)
{
if (kbhit())
{
PressedKey = getch();
printf("Times found: %d \n", TimesFound);
}
EnumWindows(EnumFunc, (LPARAM)&TimesFound);
}
我每次都使用此代码检测窗口,但是它检测到一个窗口,并且一次又一次地检测到同一窗口,因此什么都没有改变:/
在您的情况下, FindWindow
无法FindWindow
您已经看到的窗口列表,因此它将继续返回同一窗口(尽管这是实现定义的)。 无论如何, FindWindow
无法执行您要尝试的功能(在搜索过程中跳过窗口)。
如果只想枚举所有窗口一次,则应使用EnumWindows 。 我为EnumWindows
编写了一些示例C代码,该代码可以实现我想达到的目标。 您将需要将其转换为C ++,但目前对C ++的使用并不特别。
BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
/*
* We don't care about getting the whole string,
* just enough to do the comparison. GetWindowText
* will truncate the string if we tell it to.
*/
wchar_t lpString[32];
GetWindowText(hwnd, lpString, _countof);
if(wcscmp(lpString, L"Untitled - Notepad") == 0) {
(*(int *)param)++;
}
return TRUE;
}
int main(void)
{
int numFound = 0;
EnumWindows(EnumFunc, (LPARAM)&numFound);
return ERROR_SUCCESS;
}
(只需将其写在答案窗口中,这样可能会有一些小错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.