11

Consider the following code:

LPRECT lpRect;
GetWindowRect(hwnd, lpRect);

I don't know how to get information from lpRect; please advise.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
user2662326
  • 159
  • 1
  • 1
  • 7
  • 1
    Please see: http://stackoverflow.com/questions/10851278/i-cant-get-hold-of-lprect-structure-data-what-im-doing-wrong – The Floating Brain Aug 17 '13 at 18:03
  • 2
    Do you not know how to use a structure? LPRECT is just a pointer to a RECT structure, so something like lpRect->x would access a member of it. – Damon Aug 17 '13 at 18:03
  • 1
    oh my gosh wow im gonna delete this question now k? k. – user2662326 Aug 17 '13 at 18:04
  • and damon so yeah i just started coding in general, and i wasn't sure. – user2662326 Aug 17 '13 at 18:04
  • well now that its answered im not gonna delete it, and goodgood michael that is the answer. – user2662326 Aug 17 '13 at 18:05
  • 1
    If you want to accept an answer you should probably go with [datenwolf's](http://stackoverflow.com/a/18291845/1889329) submission. Michael's answer does not really address your problem (writing to random memory). – IInspectable Aug 17 '13 at 19:06
  • 1
    @GingerJack: I rolled back your edit, which was inappropriate for the following reasons: (1) it introduced a brand new bug -- wrong size passed to `malloc()` (2) it conflicted with existing answers (3) code in questions should only be edited to improve forwarding, never to fix a bug, because the bugs are an important part of the question. Even when a question contains unrelated code or problems outside the question being asked, cleanup/reduction to MCVE should be done only in answers. – Ben Voigt Dec 25 '18 at 19:22

3 Answers3

29

What you wrote is wrong. The Windows API uses a hideous variable and type naming convention. LPRECT means "Long Pointer to Rect", which on your usual architecture is just a RECT*. What you wrote is some uninitialized pointer variable, pointing at some arbitrary location (if you're unlucky something that when modified will crash your program).

This is what you actually require:

RECT rect;
GetWindowRect(hwnd, &rect);

RECT itself is a structure

typedef struct _RECT {
  LONG left;
  LONG top;
  LONG right;
  LONG bottom;
} RECT;
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • tho u super pro I didn't try it at first, then I learned the hard way. (I got "unlucky") – user2662326 Aug 19 '13 at 04:16
  • I use a laptop and the external display. All set 1920x1080 resolution. If I run my Delphi 7 program in the primray screen I get different RECTANGLE values like I run my program on the secondary display. [Pictures from source code](https://mega.nz/folder/Z5oREQxY#pgkh-jr7SbZ68E7UxAxZkw) – Lionking Jan 14 '21 at 15:55
  • @Lionking: Maybe effected by HiDPI scaling. Check if UI scaling is set to a value other than 100%. If so, set to 100% and retry if it works then. If that's the case you'll have to take that scaling into account, too: https://learn.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows – datenwolf Jan 14 '21 at 16:46
2

You can get the coordinates of the window :

lpRect->left
lpRect->right
lpRect->top
lpRect->bottom

More information here : http://msdn.microsoft.com/en-us/library/windows/desktop/dd162897(v=vs.85).aspx

Michael M.
  • 2,556
  • 1
  • 16
  • 26
-1

Or you can just type this inside your CWnd dialog:

CRect rect;
this->GetWindowRect( &rect );
Mil277
  • 1
  • 2
  • The answer does not provide additional context to what is `CRect` or even `this->GetWindowRect`. I assume it refers to MFC but it doesn't answer the original question. – Alexey Ivanov May 30 '23 at 17:00