Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC
Article

Persistent Frames

Rate me:
Please Sign up or sign in to vote.
4.63/5 (5 votes)
6 Jan 2001 92.3K   2.2K   50   5
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.
  • Download source files - 12 Kb
  • Download demo project - 64 Kb
  • Introduction

    A standard MFC SDI application has a main frame window and an MFC MDI application has a main frame and additional child frame windows. The classes presented here allow 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.

    Features:

    • Save / Restore of the position, size and minimized/maximized state of the frame window
    • Save/Restore of the position of docked control bars
    • Information is stored in the application's registry or .INI file - CWinApp::WriteProfileString() and CWinApp::WriteProfileInt() methods are used
    • Each window can have its own set of properties saved in its own registry/.INI file section
    • 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.

    How to use Persistent Frames classes

    You should include the files PersistFrameImpl.cpp, PersistFrameImpl.h, PersistMDIChildFrame.cpp, PersistMDIChildFrame.h, PersistMDIFrame.cpp, PersistMDIFrame.h, PersistSDIFrame.cpp, PersistSDIFrame.h and PersistFrames.h in your project.

    I usually include this line in my StdAfx.h file.

    #include "PersistFrames.h"

    to make all classes available everywhere in the project.

    To make your frame window persistent just follow these steps:

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

    Depending on the type of your frame window you should inherit it from different classes:

    Persistent Frames base classes
    CPersistSDIFrame A base class for SDI frame windows.
    CPersistMDIFrame A base class for MDI main frame windows.
    CPersistMDIChildFrame A base class for MDI child frame windows.

    That's it. Now compile and run your application.

    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.

    How do Persistent Frames work?

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

    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


    Written By
    Software Developer (Senior) Brosix
    Bulgaria Bulgaria
    Stefan does programming since his early ages more than 30 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.

    Comments and Discussions

     
    Generaltoolbars Pin
    25-Jul-01 6:56
    suss25-Jul-01 6:56 
    GeneralBar States Pin
    10-Jan-01 22:44
    suss10-Jan-01 22:44 
    GeneralRe: Bar States Pin
    Stefan Chekanov10-Jan-01 23:25
    Stefan Chekanov10-Jan-01 23:25 
    GeneralEasier as a lib Pin
    Bryan Anslow10-Jan-01 8:40
    Bryan Anslow10-Jan-01 8:40 
    General:( Not UNICODE Compatable Pin
    qs57791-Dec-00 3:56
    qs57791-Dec-00 3:56 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.