Click here to Skip to main content
12,943,904 members (57,860 online)
Click here to Skip to main content


51 bookmarked
Posted 12 Oct 2000

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.

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="Author" CONTENT="Stefan Chekanov">
<meta Name="description"
Content="A collection of classes that make MFC SDI and MDI applications to
remember the positions and sizes of their main frame and child frame windows.">
<meta Name="keywords" Content="MFC Frame windows">
<title>Persistent Frames</title>

<body bgcolor="#ffffff" text="#000000">

<h3 align="center"><font color="#000000">Persistent Frames</font></h3>

<p align="center">A collection of classes that make MFC SDI and MDI applications to
remember the positions and sizes of their main frame and child frame windows.</p>

<!-- Author and contact details -->

<p>This article was contributed by <a href="">Stefan Chekanov</a>.<br>
Platform: VC 5.0 &amp; VC 6.0 

  <li><a href="##intro">Introduction</a></li>
  <li><a href="##examples">How to use Persistent Frames classes</a></li>
  <li><a href="##how_works">How do Persistent Frames work?</a></li>


<p><a name="#intro"></a><big><strong>Introduction</strong></big></p>

<p>A usual MFC SDI application has main frame window and MFC MDI application has a main
frame and additional child frame windows. The classes presented here make these frame
windows to remember their last positions on the screen, their sizes and their
minimized/maximized states. So, next time the application starts, its frame windows will
restore their positions, sizes and minimized/maximized states.</p>


  <li>Save / Restore of the position, size and minimized/maximized state of the frame window</li>
  <li>Save/Restore of the position of docked control bars</li>
  <li>Information is stored in the application's registry or .INI file - <font color="#800000">CWinApp::WriteProfileString()</font>
    and <font color="#800000">CWinApp::WriteProfileInt()</font> methods are used</li>
  <li>Each window can have its own set of properties saved in its own registry/.INI file
  <li>If the user changes the screen resolution, the next time he/she starts the application
    the frames are resized to fit in new screen size.</li>


<p><a name="#examples"></a><big><strong>How to use Persistent Frames classes</strong></big></p>

<p>You should include the files <font color="#800000">PersistFrameImpl.cpp</font>, <font
color="#800000">PersistFrameImpl.h</font>, <font color="#800000">PersistMDIChildFrame.cpp</font>,
<font color="#800000">PersistMDIChildFrame.h</font>, <font color="#800000">PersistMDIFrame.cpp</font>,
<font color="#800000">PersistMDIFrame.h</font>, <font color="#800000">PersistSDIFrame.cpp</font>,
<font color="#800000">PersistSDIFrame.h</font> and <font color="#800000">PersistFrames.h</font>
in your project.</p>

<p>I usually include this line in my <font color="#800000">StdAfx.h</font> file.</p>

<pre><tt><font color="#990000">#include &quot;PersistFrames.h&quot;</font></tt></pre>

<p>to make all classes available everywhere in the project.</p>

<p>To make your frame window persistent just follow these steps: 

  <li>Inherit your frame window class from the appropriate <a href="##base_classes">Persistent
    Frames base class</a>. I usually let MFC Application or Class Wizard to make the class and
    then using the feature Find&amp;Replace replace all occurences of the base class in both <font
    color="#800000">.h</font> and <font color="#800000">.cpp</font> files.</li>
  <li>In the constructor of your frame window class call <font color="#800000">SetProfileHeading(
    LPCTSTR szHeading )</font> and specify the registry section that will be used to store
    this frame window properties. By default this isset to <em>&quot;Window size&quot;</em>.</li>
  <li>In the constructor of your frame window class call <font color="#800000">SetManageBarStates(
    bool bManage )</font> and specify whether positions of control bars should be
    saved/restored too. By default this is set to false.</li>

<p><a name="#base_classes"></a>Depending on the type of your frame window you should
inherit it from different classes:</p>

<table border="1" width="985" cellspacing="0">
    <td colspan="2" align="center" bgcolor="#C0C0C0" width="981"><strong>Persistent Frames
    base classes</strong></td>
    <td width="193"><strong>CPersistSDIFrame</strong></td>
    <td width="784">A base class for SDI frame windows.</td>
    <td width="193"><strong>CPersistMDIFrame</strong></td>
    <td width="784">A base class for MDI main frame windows.</td>
    <td width="193"><strong>CPersistMDIChildFrame</strong></td>
    <td width="784">A base class for MDI child frame windows.</td>

<p>That's it. Now compile and run your application.</p>

<p>The accompanying demo .zip file contains a VC 6.0 workspace with two projects - the one
is MFC SDI and the other is MFC MDI application.</p>


<p><a name="#how_works"></a><big><strong>How do Persistent Frames work?</strong></big></p>

<p>There is a class called <font color="#800000">CPersistFrameImpl</font> which is used in
all of the other classes. Its main methods are <font color="#800000">Load()</font> which
loads the properties from the registry and <font color="#800000">Save()</font> which saves
the properties into the registry. Each of <font color="#800000">CPersistSDIFrame</font>, <font
color="#800000">CPersistMDIFrame</font> and <font color="#800000">CPersistMDIChildFrame</font>
have an object of <font color="#800000">CPersistFrameImpl</font> and call its <font
color="#800000">Load()</font> and <font color="#800000">Save()</font> where appropriate.</p>

<p>That's all.</p>

<p><a href="">Download source</a> (12 KB)<br>
<a href="">Download demo project</a> (64 KB)</p>


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.


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


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: - 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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 7 Jan 2001
Article Copyright 2000 by Stefan Chekanov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid