它应该没有问题:
#include <iostream>
#define _WIN32_WINNT 0x501
#include <windows.h>
using namespace std;
int main() {
HWND consoleWindow = GetConsoleWindow();
LPRECT lpRect;
GetWindowRect(consoleWindow,lpRect);
cout << lpRect.top <<endl;
}
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了这个:
error: request for member 'top' in 'lpRect', which is of non-class type 'LPRECT {aka tagRECT*}'
Run Code Online (Sandbox Code Playgroud)
你的代码错了.Windows需要一个有效的Rect.LPRECT只是一个指针,你还没有初始化它.请像这样修改它.
HWND consoleWindow = GetConsoleWindow();
RECT aRect;
GetWindowRect(consoleWindow,&aRect);
cout << aRect.top <<endl;
Run Code Online (Sandbox Code Playgroud)
的LPRECT类型是一个指针到RECT。这(在我看来是不幸的)在Win32 API中很常见,它们在您身上扮演“隐藏星号”的角色。由于星号在C语言中很重要,因此会造成更多混乱。
因此,无论如何,您需要使用实际值RECT来存储结果:
RECT rect; /* An actual RECT, with space for holding a rectangle. */
/* The type of &rect is LPRECT. */
GetWindowRect(consoleWindow, &rect);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5483 次 |
| 最近记录: |