Another Method to Save/Restore Window Position/Size

.

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.

  1. When this application program is finished, write its frame window
    status – minimize, maximize or size of the window and toolbar status – in the registry.

  2. 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);
}

Download demo project – 25 Kb

More by Author

Must Read