我无法掌握LPRECT结构数据,我做错了什么?

rsk*_*k82 4 c++ winapi

它应该没有问题:

#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)

Per*_*est 6

你的代码错了.Windows需要一个有效的Rect.LPRECT只是一个指针,你还没有初始化它.请像这样修改它.

HWND consoleWindow = GetConsoleWindow();
RECT aRect;
GetWindowRect(consoleWindow,&aRect);
cout << aRect.top <<endl;
Run Code Online (Sandbox Code Playgroud)

  • @Luchian:C 函数如何更改调用者中的指针?它只能写入地址被传递的缓冲区。 (2认同)
  • @Luchian:整个 Windows API 使用 C 兼容的函数签名。没有什么比它更接近可移植性了(跨编译器、不同语言)。 (2认同)

unw*_*ind 5

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)