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



Comments

  • And for restoring from another app

    Posted by Legacy on 07/08/2003 12:00am

    Originally 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?

    Reply
  • easiest way

    Posted by Legacy on 07/02/2002 12:00am

    Originally posted by: ayach

    try this :
    
    

    void CMainFrame::OnClose()
    {
    WINDOWPLACEMENT wndpl;
    if(GetWindowPlacement(&wndpl))
    AfxGetApp()->WriteProfileBinary( _T "WindowState-Summary"),_T("WindowPlacement"), (BYTE*)&wndpl,sizeof(wndpl));
    CFrameWnd::OnClose();
    }
    ......

    BOOL CMyApp::InitInstance()
    {
    .....
    some stuff
    .....
    LPBYTE pData;
    UINT nBytes = 0;
    AfxGetApp()->GetProfileBinary(_T("WindowState-Summary"), _T("WindowPlacement"), &pData,&nBytes);
    WINDOWPLACEMENT *pwp = (WINDOWPLACEMENT*)pData;
    if(pwp)
    m_pMainWnd->SetWindowPlacement(pwp);

    m_pMainWnd->UpdateWindow();
    return TRUE;
    }

    ;o)

    Reply
  • No need to add code in CMainFrameActivateFrame()

    Posted by Legacy on 02/06/2002 12:00am

    Originally 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().

    Reply
  • Does not work for a Release Build?

    Posted by Legacy on 09/12/2001 12:00am

    Originally posted by: Joe

    Why Does'nt this work for a Release Build? in VC++ 6.0?

    Reply
  • This is a question really, not so much a 'comment' - can you help?

    Posted by Legacy on 10/07/2000 12:00am

    Originally 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:
    www.spammedia.com and www.bigbother.com

    Reply
  • How to restore position of multiple ToolBar ?

    Posted by Legacy on 05/02/2000 12:00am

    Originally 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 ?

    Reply
  • How restore window position for CMDIFrameWnd ?

    Posted by Legacy on 12/15/1999 12:00am

    Originally 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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

  • On-demand Event Event Date: August 27, 2015 With the cloud enabling companies to spin up servers and stand up data stores more quickly, mobile apps can be created faster, reducing the time-to-value. But three major obstacles stand in the way for many organizations: The backlog of app project requests confronting every enterprise regardless of their internal app development capabilities Finding and employing better, faster tools to speed and simplify the process of developing those apps. The emergence of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date