Win32: window coordinates question

Started by
6 comments, last by ts-m 16 years, 2 months ago
What is the easiest way to get the normal/restored coordinates (as screen coordinates) from a minimized or maximized window without actually restoring it? Thanks in advance
Advertisement
ah, good question... hmmm... what i would try is respond to WM_SYSCOMMAND. and when wparam = SC_MAXIMIZE or SC_MINIMIZE, i would save the current window coordinates.
Depends which language and API.

If you are using C# and WindowsForms, System.Windows.Form has a RestoreBounds property that gives the current restored rectangle of the form regardless of its current state.

If you are talking C++ and Win32API, the way I've handled this in the past is to handle WM_MOVE and WM_SIZE, since you get a WM_MOVE after a minimise, maximise or restore as well as a normal drag-move. In each event (or by passing one on to the other), I've checked to ensure the window is not minimised or maximised and then used GetWindowInfo() to retrieve the current screen rectangle of the window.

// rough exampleRECT MainRect;void OnSize(HWND Hw){    // size processing    OnMove(Hw);}void OnMove(HWND Hw){    if(IsZoomed(Hw)==TRUE || IsIconic(Hw)==TRUE) return;    WINDOWINFO Info;    GetWindowInfo(MainHw,&Info);	    MainRect=Info.rcWindow;}LRESULT CALLBACK WndProc(HWND Hw,UINT Msg,WPARAM wParam,LPARAM lParam){    switch(Msg)        {        case WM_SIZE: OnSize(Hw); break;        case WM_MOVE: OnMove(Hw); break;        default: return DefWindowProc(Hw,Msg,wParam,lParam);        }    return 0;}


This way, MainRect should always contain the restored rectangle for the window regardless of its state. This is useful when saving a window state on program close so that if you close an application when maximised, when you run it again and restore, the window can be restored to its previous position.

I'm not aware of a C++ Win32 shortcut alternative like C#'s RestoreBounds, although there may well be one since Windows has to "remember" the restore position of a window while it is maximised or minimised.
There is a function for this called GetWindowPlacement:
WINDOWPLACEMENT wp = { sizeof (WINDOWPLACEMENT) };GetWindowPlacement (hWnd, &wp);wp.rcNormalPosition is now a RECT that "specifies the window's coordinates when the window is in the restored position."


Note, those coordinates are in "workspace coordinates", not "screen coordinates", which may be different if the taskbar is docked at the left or top of the screen. Do not use SetWindowPos with those coordinates. Use SetWindowPlacement instead, to restore the window placement. MSDN has good info on this at http://msdn2.microsoft.com/en-us/library/ms633518(VS.85).aspx.
Kippesoep
Quote:Original post by Kippesoep
There is a function for this called GetWindowPlacement:
*** Source Snippet Removed ***

Note, those coordinates are in "workspace coordinates", not "screen coordinates", which may be different if the taskbar is docked at the left or top of the screen. Do not use SetWindowPos with those coordinates. Use SetWindowPlacement instead, to restore the window placement. MSDN has good info on this at http://msdn2.microsoft.com/en-us/library/ms633518(VS.85).aspx.


Thanks for that. Always thought there had to be an easier way. [smile]
Thanks. How can I convert workspace coordinates to screen coordinates for using them in other functions requiring screen coordinates for example in MonitorFromRect()?
ClientToScreen will do that. ScreenToClient works the other way round.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Nah, client coordinates are not workspace coordinates. Anyway, found solution myself: using GetMonitorInfo and then rcWork and rcMonitor members of MONITORINFO to calculate screen coordinates from workspace coordinates

This topic is closed to new replies.

Advertisement