Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Persistent Frames

, 6 Jan 2001
A collection of classes that allows MFC SDI and MDI applications to remember the positions and sizes of their main frame and child frame windows.
/*************************************************************************
 FILE       : PersistWndImpl.cpp

 Author		: Stefan Chekanov (stefant@iname.com)

 Description: Implements persistent Frame windows (MDI, MDI Child and SDI)
			  common behaviour.

Copyright(c) 1998,1999,2000
by Stefan Chekanov (stefant@iname.com)

This code may be used in compiled form in any way you desire. This
file may be redistributed unmodified by any means PROVIDING it is 
not sold for profit without the authors written consent, and 
providing that this notice and the authors name is included. If 
the source code in this file is used in any commercial application 
then a simple email to the author would be nice.

This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability if it causes any damage.

*************************************************************************/
/* #    Revisions    # */

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PersistFrameImpl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// static
TCHAR CPersistFrameImpl::s_profileHeading[]		= _T( "Window size" );
TCHAR CPersistFrameImpl::s_profileRect[]		= _T( "Rectangle" );
TCHAR CPersistFrameImpl::s_profileIcon[]		= _T( "Iconized" );
TCHAR CPersistFrameImpl::s_profileMax[]			= _T( "Maximized" );
TCHAR CPersistFrameImpl::m_profileBarState[]	= _T( "BarState" );

//////////////////////////////////////////////////////////////////////

CPersistFrameImpl::CPersistFrameImpl( CFrameWnd* pWnd, LPCTSTR szHeading /*= NULL*/, bool bBarStates /*= false*/ )
{
	m_pWnd = pWnd;

	m_wndpl.length = sizeof(WINDOWPLACEMENT);
	
	if( szHeading != NULL ) {
		SetProfileHeading( szHeading );
	}
	else {
		SetProfileHeading( s_profileHeading );
	}

	m_bManageBarStates = false;
}
//////////////////////////////////////////////////////////////////////

CPersistFrameImpl::~CPersistFrameImpl()
{
}
//////////////////////////////////////////////////////////////////////

//	Load the window state from registry and apply the new state on the window
void	CPersistFrameImpl::Load()
{
	ASSERT( m_pWnd != NULL );

    WINDOWPLACEMENT	m_wndpl;
	CString			strText;

	BOOL	bRet = m_pWnd->GetWindowPlacement( &m_wndpl );
	CRect	rect = m_wndpl.rcNormalPosition;

	//	Gets current window position and minimized/maximized status from Registry
	strText = AfxGetApp()->GetProfileString( m_profileHeading, s_profileRect );
	if( ! strText.IsEmpty() ) {
		rect.left	= _ttoi( (const char*) strText.Mid(0,4) );
		rect.top	= _ttoi( (const char*) strText.Mid(5,4) );
		rect.right	= _ttoi( (const char*) strText.Mid(10,4) );
		rect.bottom = _ttoi( (const char*) strText.Mid(15,4) );
	}

	BOOL	bIconic = AfxGetApp()->GetProfileInt( m_profileHeading, s_profileIcon, 0 );
	BOOL	bMaximized = AfxGetApp()->GetProfileInt( m_profileHeading, s_profileMax, 0 );   
    UINT flags;

	if( bIconic ) {
		m_nCmdShow = SW_SHOWMINNOACTIVE;
		if( bMaximized ) {
			flags = WPF_RESTORETOMAXIMIZED;
		}
		else {
			flags = WPF_SETMINPOSITION;
		}
	}
	else {
		if( bMaximized ) {
			m_nCmdShow = SW_SHOWMAXIMIZED;
			flags = WPF_RESTORETOMAXIMIZED;
		}
		else {
			m_nCmdShow = SW_NORMAL;
			flags = WPF_SETMINPOSITION;
		}
	}

	//	Make the window have new settings
	m_wndpl.showCmd = m_nCmdShow;
	m_wndpl.flags = flags;
	m_wndpl.ptMinPosition = CPoint(0, 0);
	m_wndpl.ptMaxPosition = CPoint( -::GetSystemMetrics(SM_CXBORDER),
									-::GetSystemMetrics(SM_CYBORDER) );
	m_wndpl.rcNormalPosition = rect;

	m_pWnd->SetWindowPlacement( &m_wndpl );

	if( m_bManageBarStates )
	{
		CString	cTmp;
		cTmp.Format( "%s\\%s", m_profileHeading, m_profileBarState );
		m_pWnd->LoadBarState( cTmp );
	}
}
//////////////////////////////////////////////////////////////////////

//	Save the window state into registry
void	CPersistFrameImpl::Save()
{
	ASSERT( m_pWnd != NULL );

	CString			strText;
	BOOL			bIconic = FALSE;
	BOOL			bMaximized = FALSE;

	//	Gets current window position and minimized/maximized status
	BOOL	bRet = m_pWnd->GetWindowPlacement( &m_wndpl );
	if( m_wndpl.showCmd == SW_SHOWNORMAL ) {
		bIconic = FALSE;
		bMaximized = FALSE;
	}
	else if( m_wndpl.showCmd == SW_SHOWMAXIMIZED) {
		bIconic = FALSE;
		bMaximized = TRUE;
	} 
	else if( m_wndpl.showCmd == SW_SHOWMINIMIZED) {
		bIconic = TRUE;
		if( m_wndpl.flags ) {
			bMaximized = TRUE;
		}
		else {
			bMaximized = FALSE;
		}
	}
	
	//	Save the state into Registry
	strText.Format( "%04d %04d %04d %04d",
			m_wndpl.rcNormalPosition.left,
			m_wndpl.rcNormalPosition.top,
			m_wndpl.rcNormalPosition.right,
			m_wndpl.rcNormalPosition.bottom );
	AfxGetApp()->WriteProfileString( m_profileHeading, s_profileRect, strText );
	AfxGetApp()->WriteProfileInt( m_profileHeading, s_profileIcon, bIconic );
	AfxGetApp()->WriteProfileInt( m_profileHeading, s_profileMax, bMaximized );

	if( m_bManageBarStates )
	{
		CString	cTmp;
		cTmp.Format( "%s\\%s", m_profileHeading, m_profileBarState );
		m_pWnd->SaveBarState( cTmp );
	}
}
//////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Stefan Chekanov
Software Developer (Senior) Brosix
Bulgaria Bulgaria
Stefan does programming since his early ages more than 20 years ago. Later he began programming at his work and now he is very happy to work his hobby. He owns a Master's degree in Computer Science.
 
During his professional career, Stefan has worked with many technologies. His programming experience includes C/C++, Java, C#, PHP, JavaScript, MFC, ATL, ASP, ASP.NET, TCP/IP, SQL. He has worked with different operating systems: Windows, Linux, Mac OS X, iOS, Android, Solaris, FreeBSD, NetBSD and QNX.
 
Currently his professional interests are in building large scale distributed systems that operate over the Internet. This involves building server components as well as developing system level software.

More information about his current work can be found here: brosix.com - Enterprise Instant Messaging
 

Stefan is based in Plovdiv, Bulgaria. It is a very nice and peaceful place combined with an enjoyable weather.
 
Stefan has a wife and 2 children. He likes in his spear time to travel with his family to see new and interesting places.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 7 Jan 2001
Article Copyright 2000 by Stefan Chekanov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid