我想开发一个程序,它的 ID 是一张卡片,因为它在另一个正在运行的程序(例如扑克或红心游戏或其他任何程序)中播放。我首先尝试获取我需要的关于已经运行的游戏程序的信息,但我从一开始就遇到了问题。我正在运行 MSVC++ 2013 并开发 MFC 应用程序。现在我正在玩 Hearts 游戏,这是代码:
HWND hwnd = FindWindowA(NULL, "Hearts");
if (hwnd == NULL)
{ /* window not found*/
}
else
{ /* window was found */
RECT rect;
GetWindowRect(hwnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
}
Run Code Online (Sandbox Code Playgroud)
所以我得到 hwnd 就好了,代码的工作取决于我是否打开了 Hearts。但该行GetWindowRect(hwnd, &rect);
不会编译说
“错误 C2660:‘CWnd::GetWindowRect’:函数不接受 2 个参数”。
有一个GetWindowRect
函数只有 rect 参数,但获取我正在处理的程序窗口的属性。有很多文档GetWindowRect
显示了上述两个参数,但是如何调用该子例程?
小智 5
当您在 MFC 窗口类中时,您正在调用该CWnd::GetWindowRect
函数 - 您想调用 Win32 API 中的函数,因此:
::GetWindowRect(hwnd, &rect);
Run Code Online (Sandbox Code Playgroud)
其中::
范围解析运算符(左侧没有命名空间或类名)表示在全局范围内调用函数。