Another Method to Save/Restore Window Position/Size | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 15, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.