Restoring Window Position
Posted
by Yonat Sharon
on August 6th, 1998
Saving the window's position is straight forward:
void CMainFrame::OnClose() // message handler for WM_CLOSE
{
// Save main window position
CWinApp* app = AfxGetApp();
WINDOWPLACEMENT wp;
GetWindowPlacement(&wp);
app->WriteProfileInt("Frame", "Status", wp.showCmd);
app->WriteProfileInt("Frame", "Top", wp.rcNormalPosition.top);
app->WriteProfileInt("Frame", "Left", wp.rcNormalPosition.left);
app->WriteProfileInt("Frame", "Bottom", wp.rcNormalPosition.bottom);
app->WriteProfileInt("Frame", "Right", wp.rcNormalPosition.right);
CFrameWnd::OnClose();
}
However, restoring it is a little more tricky:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
//
// Restore main window position
//
CWinApp* app = AfxGetApp();
int s, t, b, r, l;
// only restore if there is a previously saved position
if ( -1 != (s = app->GetProfileInt("Frame", "Status", -1)) &&
-1 != (t = app->GetProfileInt("Frame", "Top", -1)) &&
-1 != (l = app->GetProfileInt("Frame", "Left", -1)) &&
-1 != (b = app->GetProfileInt("Frame", "Bottom", -1)) &&
-1 != (r = app->GetProfileInt("Frame", "Right", -1))
) {
// restore the window's status
app->m_nCmdShow = s;
// restore the window's width and height
cs.cx = r - l;
cs.cy = b - t;
// the following correction is needed when the taskbar is
// at the left or top and it is not "auto-hidden"
RECT workArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
l += workArea.left;
t += workArea.top;
// make sure the window is not completely out of sight
int max_x = GetSystemMetrics(SM_CXSCREEN) -
GetSystemMetrics(SM_CXICON);
int max_y = GetSystemMetrics(SM_CYSCREEN) -
GetSystemMetrics(SM_CYICON);
cs.x = min(l, max_x);
cs.y = min(t, max_y);
}
return CFrameWnd::PreCreateWindow(cs);
}
Date Posted: 6/24/98
Posted by: Pat Laplante.

Comments
MDI Child sized outside main frame
Posted by Legacy on 10/23/2003 12:00amOriginally posted by: m hayes
ReplyRemoving flicker when changing window size/position on creation
Posted by Legacy on 11/07/2002 12:00amOriginally posted by: Peter Robertson
ReplyA link to Jeff Prosise's article "Building Sticky Windows"
Posted by Legacy on 11/20/1999 12:00amOriginally posted by: Yuri Osipov
Here is a link to profound article by Jeff Prosise
"Building Sticky Windows" on the topic.
Besides of the comments on the source very much alike to
Sharon's one there are comments on the role of the
'flags' member of WINDOWPLACEMENT structure and reaction
on WM_ENDSESSION message.
http://search.zdnet.com/pcmag/pctech/content/14.19/pp1419.001.html
ReplyRestoring Window Position
Posted by Legacy on 04/15/1999 12:00amOriginally posted by: Pierre Morency
Very nice piece of code.
ReplyTrapping a "PreCreateWindow" call and going through a CREATESTRUCT to set the main window position, size and state was not obvious at all.
Thanks again.
problem in restoring the window size.
Posted by Legacy on 04/09/1999 12:00amOriginally posted by: madan
i had tried with the code,i pop up the dialog box first,
Reply& then SDI,it is working with the dialog box,but with that
i also want to restore the size of SDI mainframe ,
though ,i am a new programmer in VC++,i need ur help,
please ,solve my problem,
help!!!!!!!
madan.
Nice piece of code.
Posted by Legacy on 03/26/1999 12:00amOriginally posted by: Kurt P. Gulden
Just wanted to let you know that this is a nice piece of work. It droppped-right in to my MDI application. Cool!
ReplyHow to restore the CMDIChildWnd?
Posted by Legacy on 03/11/1999 12:00amOriginally posted by: nadir
Hi,
ReplyIn my program, I have tried your code, it is fine to the main frame.
While to the child frame, I could not find a proper place to deal with that
// restore the window's status
app->m_nCmdShow = s;
How can I?
Yours.
creeps with CDialog
Posted by Legacy on 01/09/1999 12:00amOriginally posted by: ljp
I plugged in the x and y to SetWindowPos on a CDialog App, and it creeps upward. Works great with Window App.
ReplyHow to over come the Flicker effects of the window creation ?
Posted by Legacy on 11/11/1998 12:00amOriginally posted by: Senthil
Reply