Another Method to Save/Restore Window Position/Size
Posted
by HanYong Kim
on May 15th, 1999
A smart application program should be recover its original frame window status when it is run again. There already exists article on Saving and restoring window positions that has the same effect with my code, except that the article does not consider toolbar status. Calling the toolbar status restore function (LoadBarState) is impossible in 'PreCreateWindow' function. This article gets around this problem.
- When this application program is finished, write its frame window status - minimize, maximize or size of the window and toolbar status - in the registry.
- When this application program is started, read its registry value and control frame window and toolbar status.
Simple coding in the 2 functions of CMainFrame class is needed to do the above tasks.
void CMainFrameOnClose()
{
// TODO Add your message handler code here and/or call default
WINDOWPLACEMENT WndStatus;
WndStatus.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(&WndStatus);
AfxGetApp()->WriteProfileInt("WNDSTATUS","FLAG",WndStatus.flags);
AfxGetApp()->WriteProfileInt("WNDSTATUS","SHOWCMD",WndStatus.showCmd);
AfxGetApp()->WriteProfileInt("WNDSTATUS", "LEFT",
WndStatus.rcNormalPosition.left);
AfxGetApp()->WriteProfileInt("WNDSTATUS", "TOP",
WndStatus.rcNormalPosition.top);
AfxGetApp()->WriteProfileInt("WNDSTATUS", "RIGHT",
WndStatus.rcNormalPosition.right);
AfxGetApp()->WriteProfileInt("WNDSTATUS", "BOTTOM",
WndStatus.rcNormalPosition.bottom);
SaveBarState(AfxGetApp()->m_pszProfileName);
CFrameWndOnClose();
}
void CMainFrameActivateFrame(int nCmdShow)
{
// TODO Add your specialized code here and/or call the base class
if(m_bFirst)
{
m_bFirst=FALSE;
WINDOWPLACEMENT WndStatus;
WndStatus.length = sizeof(WINDOWPLACEMENT);
CRect rect;
rect.left = AfxGetApp()->GetProfileInt("WNDSTATUS", "LEFT", 100);
rect.top = AfxGetApp()->GetProfileInt("WNDSTATUS", "TOP", 100);
rect.right = AfxGetApp()->GetProfileInt("WNDSTATUS", "RIGHT", 500);
rect.bottom = AfxGetApp()->GetProfileInt("WNDSTATUS", "BOTTOM", 400);
WndStatus.rcNormalPosition = rect;
WndStatus.flags = AfxGetApp()->GetProfileInt("WNDSTATUS", "FLAG",0);
WndStatus.showCmd = nCmdShow = AfxGetApp()->GetProfileInt("WNDSTATUS", "SHOWCMD",SW_SHOW);
WndStatus.ptMinPosition = CPoint(0,0);
WndStatus.ptMaxPosition = CPoint(-GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
LoadBarState(AfxGetApp()->m_pszProfileName);
SetWindowPlacement(&WndStatus);
}
CFrameWndActivateFrame(nCmdShow);
}

Comments
And for restoring from another app
Posted by Legacy on 07/08/2003 12:00amOriginally posted by: David
Could it be posiible in order to get the same presentation that the register you save would be able to be open from another app?
Replyeasiest way
Posted by Legacy on 07/02/2002 12:00amOriginally posted by: ayach
ReplyNo need to add code in CMainFrameActivateFrame()
Posted by Legacy on 02/06/2002 12:00amOriginally posted by: Melwyn
Since the toolbar status restore function (LoadBarState) cannot be called in 'PreCreateWindow', the author has called it in 'CMainFrameActivateFrame'....and i suspect the variable 'm_bFirst' is used to determine if it is the first showing.
However the function 'LoadBarState' can be called in CMainFrame::OnCreate() function. Just call the function before returning from OnCreate().
ReplyDoes not work for a Release Build?
Posted by Legacy on 09/12/2001 12:00amOriginally posted by: Joe
Why Does'nt this work for a Release Build? in VC++ 6.0?
ReplyThis is a question really, not so much a 'comment' - can you help?
Posted by Legacy on 10/07/2000 12:00amOriginally posted by: Steen Bentall
Q:
I'm making a cd using Flash 4.0. It will be full screen and therefore lack the usual minimize, resize and close buttons that you normally find at the top right of every self respecting pc window.
How do you make a minimize button in Flash which, when pressed will dock a full screen projector exe file into the bottom menu bar of a windows pc? (and then spring back to full screen again when you want it to).
I would very much appreciate it if you could help.
By way of a comment; I would like to thank technology for all this excellent coding. Without it I wouldn't be able to do anything.
Yours sincerely,
S. Bentall
P.S. Check out my web pages if you like:
Replywww.spammedia.com and www.bigbother.com
How to restore position of multiple ToolBar ?
Posted by Legacy on 05/02/2000 12:00amOriginally posted by: ROULET Sylvain
LoadBarState / SaveBarState is ok to restore position of ToolBar, but it doesn't work if there is more than on ToolBar in the main frame.
Moreover it cause an assertion fault when restoring position, if ToolBars are in Floating position.
Any solution ?
ReplyHow restore window position for CMDIFrameWnd ?
Posted by Legacy on 12/15/1999 12:00amOriginally posted by: Jan Kowalski
How restere window position for class
class CMainFrame : public CMDIFrameWnd
Function "virtual void ActivateFrame(int nCmdShow = -1);" is not started. Why ?
Reply