It should be working without issue:
#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;
}
but instead I get this:
error: request for member 'top' in 'lpRect', which is of non-class type 'LPRECT {aka tagRECT*}'
Your code is wrong. Windows expects a valid Rect here. LPRECT is just a pointer and you haven't initialized it. Please modify it like this.
HWND consoleWindow = GetConsoleWindow();
RECT aRect;
GetWindowRect(consoleWindow,&aRect);
cout << aRect.top <<endl;
The LPRECT
type is a pointer to RECT
. This is (unfortunately, in my opinion) common in the Win32 API, that they play "hide the asterisk" on you. It makes for more confusion, since the asterisk is important in C.
So, anyway, you need to use an actual RECT
to have somewhere to store the result:
RECT rect; /* An actual RECT, with space for holding a rectangle. */
/* The type of &rect is LPRECT. */
GetWindowRect(consoleWindow, &rect);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With