Saving/restoring mainframe & view positions/sizes 
Author Message
 Saving/restoring mainframe & view positions/sizes

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
        CFrameWnd::OnSize(nType, cx, cy);
        //CWnd::OnSize(nType,cx,cy);
        if (!CWnd::IsIconic())
        {

        RECT rect;
        rect.left=0;
        rect.right=0;
        rect.top=0;
        rect.bottom=0;
        GetWindowRect(&rect);

        int wid=rect.right-rect.left;
        int hei=rect.bottom-rect.top;

        char height[10],width[10];

        itoa(hei,height,10);
        itoa(wid,width,10);

        WritePrivateProfileString("Size","Width",width,"Surfpos.ini");
        WritePrivateProfileString("Size","Height",height,"Surfpos.ini");
        }      

Quote:
}

void CMainFrame::OnMove(int x, int y)
{
        CFrameWnd::OnMove(x, y);
        //CWnd::OnMove(x,y);

        RECT rect;
        rect.left=0;
        rect.right=0;
        rect.top=0;
        rect.bottom=0;
        GetWindowRect(&rect);

        char horizpos[10],vertpos[10];
        itoa(rect.left,horizpos,10);
        itoa(rect.top,vertpos,10);

        WritePrivateProfileString("Positions","Horizontal",horizpos,"Surfpos.ini");
        WritePrivateProfileString("Positions","Vertical",vertpos,"Surfpos.ini");

Quote:
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{

        int width,height,horizpos,vertpos;
        char cwidth[10],cheight[10],choriz[10],cvert[10];
        GetPrivateProfileString("Positions","Horizontal","0",choriz,sizeof(choriz
),"surfpos.ini");
        GetPrivateProfileString("Positions","Vertical","0",cvert,sizeof(cvert),"s
urfpos.ini");
        GetPrivateProfileString("Size","Width","50",cwidth,sizeof(cwidth),"surfpo
s.ini");
        GetPrivateProfileString("Size","Height","50",cheight,sizeof(cheight),"sur
fpos.ini");
        width=atoi(cwidth);
        height=atoi(cheight);
        horizpos=atoi(choriz);
        vertpos=atoi(cvert);
        cs.cx=width;
        cs.cy=height;

        cs.x=horizpos;
        cs.y=vertpos;

        return CFrameWnd::PreCreateWindow(cs);

Quote:
}

This works for me....should work for you too...

C. Zieler



Tue, 27 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
Hello,

I noticed that your example doesn't check to see if the window is maximized
or not.  I have been having a similar problem and tried to use
GetWindowPlacement to check the maximized/normal status of the MDI child
windows.  However, one of my windows always returns normal, even if it is
maximized.

Do you know a way around this?

Thanks,

Chip



Tue, 27 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
sure sorry about that...I didnt have the most recent version of code on this
computer I am at now..
try this
void CMainFrame::OnMove(int x, int y)
{
        CFrameWnd::OnMove(x, y);
        if (!CWnd::IsIconic())
        {
        RECT rect;
        rect.left=0;
        rect.right=0;
        rect.top=0;
        rect.bottom=0;
        GetWindowRect(&rect);

        char horizpos[10],vertpos[10];
        itoa(rect.left,horizpos,10);
        itoa(rect.top,vertpos,10);

        WritePrivateProfileString("Positions","Horizontal",horizpos,"Surfpos.ini");
        WritePrivateProfileString("Positions","Vertical",vertpos,"Surfpos.ini");
        }

Quote:
}

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
        CFrameWnd::OnSize(nType, cx, cy);
        if (!CWnd::IsIconic())
        {
        RECT rect;
        rect.left=0;
        rect.right=0;
        rect.top=0;
        rect.bottom=0;
        GetWindowRect(&rect);

        int wid=rect.right-rect.left;
        int hei=rect.bottom-rect.top;

        char height[10],width[10];

        itoa(hei,height,10);
        itoa(wid,width,10);

        WritePrivateProfileString("Size","Width",width,"Surfpos.ini");
        WritePrivateProfileString("Size","Height",height,"Surfpos.ini");

        }

Quote:
}



Tue, 27 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
oh sorry that might not work that one checks to see if its minimized


Tue, 27 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes

Quote:
>oh sorry that might not work that one checks to see if its minimized

Right.  If the window were maximized before shutdown, it would be redrawn to
occupy the entire client area of the main fram window, but it wouldn't
really be maximized.  You wouldn't see the child windows title bar, but the
minimize, restore and close buttons wouldn't be displayed on the menu bar of
the parent window either.

Any ideas on how to add that ability?

Thanks,

Chip



Tue, 27 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
Hi Greg,

Try this:

BOOL ReadWindowPlacement( LPCTSTR lpWindow, LPWINDOWPLACEMENT pWP )
{
 CString strBuffer=
  AfxGetApp()->GetProfileString( _T("Settings\\Windows"), lpWindow );
 if ( strBuffer.IsEmpty() )
 {
  return( FALSE );
 }

 TCHAR szFormat[]= _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
 WINDOWPLACEMENT wp;
 int nRead= _stscanf( strBuffer, szFormat,
  &wp.flags, &wp.showCmd,
  &wp.ptMinPosition.x, &wp.ptMinPosition.y,
  &wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
  &wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
  &wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom );

 if ( nRead != 10 )
 {
  return( FALSE );
 }

 wp.length= sizeof( wp );
 *pWP= wp;
 return( TRUE );

Quote:
}

void WriteWindowPlacement( LPCTSTR lpWindow, LPWINDOWPLACEMENT pWP )
{
 TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];

 TCHAR szFormat[]= _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
 wsprintf( szBuffer, szFormat,
  pWP->flags, pWP->showCmd,
  pWP->ptMinPosition.x, pWP->ptMinPosition.y,
  pWP->ptMaxPosition.x, pWP->ptMaxPosition.y,
  pWP->rcNormalPosition.left, pWP->rcNormalPosition.top,
  pWP->rcNormalPosition.right, pWP->rcNormalPosition.bottom );
 AfxGetApp()->WriteProfileString(  _T("Settings\\Windows"), lpWindow,
szBuffer );

Quote:
}

void CMainFrame::OnClose()
{
 // Check if user wants to save application settings.
 if ( theApp.m_cfg.GetSaveSettingsFlag() )
 {
  // Save the main windows position.
  WINDOWPLACEMENT wp;
  wp.length= sizeof( wp );
  if ( GetWindowPlacement( &wp ) )
  {
   wp.flags= 0;
   if ( IsZoomed() )
   {
    wp.flags |= WPF_RESTORETOMAXIMIZED;
   }
   // and write it to the .INI file
   ::WriteWindowPlacement( _T("Main"), &wp );
  } // if ( GetWindowPlacement( &wp ) )
  // Save the rest of the application settings.
  theApp.m_cfg.SaveSettings();
  SaveBarState( _T("ToolBars\\") );
 } // if ( theApp.m_cfg.GetSaveSettingsFlag() )
 CMDIFrameWnd::OnClose();

Quote:
}

int CMainFrame::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
 if ( CMDIFrameWnd::OnCreate( lpCreateStruct ) == -1 )
 {
  return( -1 );
 }

 WINDOWPLACEMENT wp;
 if ( ::ReadWindowPlacement( _T("Main"), &wp ) )
 {
  SetWindowPlacement( &wp );
 }

 if ( !m_wndMainBar.Create( this ) ||
  !m_wndMainBar.LoadToolBar( IDR_MAINFRAME ) )
 {
  TRACE0( _T("Failed to create toolbar\n") );
  return( -1 );      // fail to create
 }
 m_wndMainBar.SetBarStyle( m_wndMainBar.GetBarStyle()|
  CBRS_TOOLTIPS| CBRS_FLYBY| CBRS_SIZE_DYNAMIC );
 m_wndMainBar.SetWindowText( _T("Main Toolbar") );
 m_wndMainBar.EnableDocking( CBRS_ALIGN_ANY );

 if ( !m_wndUsrMgrBar.Create( this,
  WS_CHILD| WS_VISIBLE| CBRS_TOP, IDR_SC3USRMGR ) ||
  !m_wndUsrMgrBar.LoadToolBar( IDR_SC3USRMGR ) )
 {
  TRACE0( _T("Failed to create User Manager toolbar\n") );
  return( -1 );      // fail to create
 }
 m_wndUsrMgrBar.SetBarStyle( m_wndUsrMgrBar.GetBarStyle()|
  CBRS_TOOLTIPS| CBRS_FLYBY| CBRS_SIZE_DYNAMIC );
 m_wndUsrMgrBar.SetWindowText( _T("User Manager Toolbar") );
 m_wndUsrMgrBar.EnableDocking( CBRS_ALIGN_ANY );

 if ( !m_wndStatusBar.Create( this ) ||
  !m_wndStatusBar.SetIndicators( indicators,
  sizeof( indicators ) / sizeof( UINT ) ) )
 {
  TRACE0( _T("Failed to create status bar\n") );
  return( -1 );      // fail to create
 }

 EnableDocking( CBRS_ALIGN_ANY );
 DockControlBar( &m_wndUsrMgrBar );
 DockControlBarLeftOf( &m_wndMainBar, &m_wndUsrMgrBar );
 LoadBarState( _T("ToolBars\\") );
 // The User Manager control bar should not be visible right now.
 ShowControlBar( &m_wndUsrMgrBar, FALSE, FALSE );

 return( 0 );

Quote:
}

void CMainFrame::OnMove( int x, int y )
{
 CMDIFrameWnd::OnMove( x, y );
 // Check if user wants to save application settings.
 if ( theApp.m_cfg.GetSaveSettingsFlag() )
 {
  // Save the main windows position.
  WINDOWPLACEMENT wp;
  wp.length= sizeof( wp );
  if ( GetWindowPlacement( &wp ) )
  {
   wp.flags= 0;
   if ( IsZoomed() )
   {
    wp.flags |= WPF_RESTORETOMAXIMIZED;
   }
   // and write it to the .INI file
   ::WriteWindowPlacement( _T("Main"), &wp );
  } // if ( GetWindowPlacement( &wp ) )
 } // if ( theApp.m_cfg.GetSaveSettingsFlag() )

Quote:
}

void CMainFrame::OnSize( UINT nType, int cx, int cy )
{
 CMDIFrameWnd::OnSize( nType, cx, cy );
 // Check if user wants to save application settings.
 if ( theApp.m_cfg.GetSaveSettingsFlag() )
 {
  // Save the main windows position.
  WINDOWPLACEMENT wp;
  wp.length= sizeof( wp );
  if ( GetWindowPlacement( &wp ) )
  {
   wp.flags= 0;
   if ( IsZoomed() )
   {
    wp.flags |= WPF_RESTORETOMAXIMIZED;
   }
   // and write it to the .INI file
   ::WriteWindowPlacement( _T("Main"), &wp );
  } // if ( GetWindowPlacement( &wp ) )
 } // if ( theApp.m_cfg.GetSaveSettingsFlag() )

Quote:
}

You can also use it to save the views through their frame class.

HTH
--
=================
Frank Hickman
Computers Plus


=================



Wed, 28 Feb 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
Try using the functions:
    CWnd::GetWindowPlacement
and
    CWnd::SetWindowPlacement
Quote:

>Hello,

>I noticed that your example doesn't check to see if the window is maximized
>or not.  I have been having a similar problem and tried to use
>GetWindowPlacement to check the maximized/normal status of the MDI child
>windows.  However, one of my windows always returns normal, even if it is
>maximized.

>Do you know a way around this?

>Thanks,

>Chip



Thu, 01 Mar 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
Hello,

Thanks for the suggestion.  However, if you reread my message, you will see
that GetWindowPlacement does not function correctly.

Chip

Quote:

>Try using the functions:
>    CWnd::GetWindowPlacement
>and
>    CWnd::SetWindowPlacement


>>Hello,

>>I noticed that your example doesn't check to see if the window is
maximized
>>or not.  I have been having a similar problem and tried to use
>>GetWindowPlacement to check the maximized/normal status of the MDI child
>>windows.  However, one of my windows always returns normal, even if it is
>>maximized.

>>Do you know a way around this?

>>Thanks,

>>Chip



Thu, 01 Mar 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes

Quote:
>I noticed that your example doesn't check to see if the window is maximized
>or not.  I have been having a similar problem and tried to use
>GetWindowPlacement to check the maximized/normal status of the MDI child
>windows.  However, one of my windows always returns normal, even if it is
>maximized.

>Do you know a way around this?

void StoreWndPlacement( CWnd&  wnd, WINDOWPLACEMENT& wndpl )
{
    wndpl.length = sizeof(WINDOWPLACEMENT);
    wnd.GetWindowPlacement( &wndpl );
    if ( wnd.IsZoomed() )
        wndpl.flags |= WPF_RESTORETOMAXIMIZED;

Quote:
}

When restoring MDI frames window placement, it is not enough just to
read back previously saved WINDOWPLACEMENT structures and
call SetWindowPlacement(); if you do it for an MDI frame with a flag
WPF_RESTORETOMAXIMIZED set, it will be restored to maximized
state. However, any calls to SetWindowPlacement() for _subsequent_
frames (that are not maximized) will _reset_ that state.

Perhaps you can do some of the following:
1. When storing info, check to see if any of the MDI frames is in
maximized state; if so, save WPF_RESTORETOMAXIMIZED flag
with _every_ window placement info.
2. Store WPF_RESTORETOMAXIMIZED flag only with the window that
is found maximized. Then, in loading time, apply that flag to _all_
subsequent MDI frames after you find a frame with that flag set:
...
if ( wndpl.flags & WPF_RESTORETOMAXIMIZED )
    m_bContinueWithMaximizedState = true;
if ( m_bContinueWithMaximizedState )       // for subsequent frames
    wndpl.flags |= WPF_RESTORETOMAXIMIZED;
...



Fri, 02 Mar 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes
Vladimir,

Quote:
>Perhaps you can do some of the following:
>1. When storing info, check to see if any of the MDI frames is in
>maximized state; if so, save WPF_RESTORETOMAXIMIZED flag
>with _every_ window placement info.
>2. Store WPF_RESTORETOMAXIMIZED flag only with the window that
>is found maximized. Then, in loading time, apply that flag to _all_
>subsequent MDI frames after you find a frame with that flag set:

I messed around with this by simply hard coding the maximized window
position to test your theory.  I found something interesting.  The windows
are drawn with dimensions that suggest they are maximized.  However, the
minimize, maximize/restore and close buttons are not drawn on the menu bar
of the main MDI frame window.   If I use the Window menu to switch to the
next open window, the buttons are drawn.

Am I missing something?  Do I have to do something specific to my child
window to have the buttons drawn?

Thanks,

Chip



Sun, 04 Mar 2001 03:00:00 GMT  
 Saving/restoring mainframe & view positions/sizes

Quote:
>I messed around with this by simply hard coding the maximized window
>position to test your theory.  I found something interesting.  The windows
>are drawn with dimensions that suggest they are maximized.  However, the
>minimize, maximize/restore and close buttons are not drawn on the menu bar
>of the main MDI frame window.   If I use the Window menu to switch to the
>next open window, the buttons are drawn.

I cannot guess what could be the cause of problem, but there are a plenty
of ways to force window updating - perhaps you could try with them for a
start. Regarding the theory, here is a trivial version of it (in real life
there are a lot more to handle - such as frame Z order, views,
splitters etc.):

void CMyMDIApp::StoreFramePlacement( CDocument * pDocument, CArchive& ar )
{
    POSITION posView = pDocument->GetFirstViewPosition();
    while ( posView != NULL )
    {
        CView * pView = pDocument->GetNextView( posView );

        WINDOWPLACEMENT wndpl;
        wndpl.length = sizeof WINDOWPLACEMENT;
        pView->GetParentFrame()->GetWindowPlacement( &wndpl );
        if ( pWnd->IsZoomed() )
            wndpl.flags |= WPF_RESTORETOMAXIMIZED;
        ar << wndpl;        // write your own or use Write()
    }

Quote:
}

In this way all the MDI frames for a certain document will be
serialized: maximized one is stored with
WPF_RESTORETOMAXIMIZED flag, and others are stored
without it.

When restoring from archive (again, in a very simplified version),
you could do the following:

void CMyMDIApp::LoadFramePlacement( CDocument * pDocument, CArchive& ar )
{
    bool bContinueMaximized = false;
    POSITION posView = pDocument->GetFirstViewPosition();
    while ( posView != NULL )
    {
        WINDOWPLACEMENT wndplFrame;
        ar >> wndplFrame;    // write your own operator or use Read()

        // after you find a frame that should be maximized, you must apply
        // that flag to all subsequent frames - use bContinueMaximized
        // to indicate it
        if ( wndplFrame.flags & WPF_RESTORETOMAXIMIZED )
            bContinueMaximized = true;
        // even if current frame was not stored with maximized flag, it
        // it will be forced to maximized state here if any of previous
        // frames was maximized
        if ( bContinueMaximized )
            wndplFrame.flags |= WPF_RESTORETOMAXIMIZED;
        CView *pView = pDocument->GetNextView( posView );
        pView->GetParentFrame()->SetWindowPlacement( &wndplFrame );
    }

Quote:
}



Thu, 08 Mar 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Saving Window Size & Position

2. Restoring View Size in Split Views :: MFC

3. How to save/restore toolbar positions?

4. save/restore splitter position

5. Maximized position Save/Restore works ONLY on WinNT!

6. Can I save and restore docking toolbar position?

7. Saving/Restoring CToolBar's old position

8. Save and Restore MDI Window Positions

9. restoring a window size and position

10. Save and Restore Window Size.

11. q: utility for saving & restoring devstudio settings

12. Saving Child Window Size, Position, Content

 

 
Powered by phpBB® Forum Software