Sign in to follow this  
ts-m

Win32: window coordinates question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 example

RECT 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Thanks. How can I convert workspace coordinates to screen coordinates for using them in other functions requiring screen coordinates for example in MonitorFromRect()?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this