sazr
sazr

Reputation: 25928

Get Window's Current Dimensions regardless of minimise/normal/maximise state

I am trying to obtain a 'top level' HWND's dimensions. Ie, I want the current dimensions of the main HWND of the Firefox/Windows Explorer/etc. window.

GetWindowRect() wont work if the window is minimised. GetWindowPlacement() works for most cases (incl minimised) but it returns the windows' SW_SHOWNORMAL dimensions. So if I resize Windows Explorer down to 100 pixels by 100 pixels, maximise the window then call GetWindowPlacement() I will get the SW_SHOWNORMAL dimensions of 100x100. I want the maximised dimensions, ie, the current dimensions of the window.

Is there a one size fits all WinAPI function that will meet all my cases? Ie, tell me the current window dimensions when its visible (regardless if it is right now)? If not I guess I'm going to have to call GetWindowPlacement(), determine if the window is minimised. If true, use the normal dimensions otherwise call GetWindowRect().

Upvotes: 2

Views: 1650

Answers (2)

GSP
GSP

Reputation: 584

Use IsIconic(hwnd) to determine state of current window handle:

  1. If normal/maximize state. Use GetWindowRect(hwnd, &rect) to get its dimensions.
  2. If minimize state, you should restore the window to get its dimensions like this "3 & 5 - Restore/Minimize The Window"

Update: Using IsIconic() instead of comparing rect(Left,Top) with -32000 to determine window handle state which are commented by @IInspectable and @Raymond Chen.

Upvotes: 0

Chris Becke
Chris Becke

Reputation: 36026

GetWindowPlacement will fill in a WINDOWPLACEMENT structure with the minimize and maximized positions as well as the normal position of the window.

It is crucial to note that the window positions returned are in workspace - not screen - co-ordinates, and so should be used in calls that accept workspace rather than screen co-ordinates.

Practically this means that if the user has positioned their taskbar across the top of the screen then "0,0" corresponds to the top left pixel below the taskbar.

In terms of getting the dimensions of the window when maximized, they would correspond to the dimensions of the monitor the window is maximized on. In that case there seems no single API call that can answer this question, as the question doesn't really make sense anymore: Why would you want the maximized size when maximized but the "normal" size when minimized? The user is just as likely to restore the window to a maximized or normal state.

Upvotes: 3

Related Questions