Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming / MFC
Article

Save/Restore main window size with a single function call

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
13 Sep 2000 104.1K   1.6K   24   9
Save/restore the main window size at application exit/startup with a single function call in MDI, SDI and dialog based applications.
  • Download demo project - 23 Kb
  • Download source - 6 Kb
  • Introduction

    Saving the main window size at application exit and restoring it at startup time is not difficult, but inconvenient, because SDI,MDI and dialog based applications have to be handled differently. The class CMainWndPlacement described in this article reduces this coding task to a single function call.

    How to use it

    1. Add Subclass.h Subclass.cpp and MainWndPlacement.h to your project.
    2. Add #include "MainWndPlacement.h" in the implementation file of your application class (MDI,SDI) or dialog class (dialog based application).
    3. For MDI/SDI applications replace in InitInstance the call pMainFrame->ShowWindow(m_nCmdShow); by CMainWndPlacement::InitialShow(m_nCmdShow);. For dialog based applications add in OnInitDialog the call CMainWndPlacement::InitialShow();.

    How it works

    The magic, that a single function call is sufficient for the save/restore task which has to be done at program startup and and program exit time, is due to the use of a window hook. The call to CMainWndPlacement::InitialShow generates an object which manages it to get all window messages for the main window. As soon as this object gets the WM_DESTROY message, it saves the current window placement using the WriteProfileString function. Later on, when the WM_NCDESTROY message arrives, the object deletes itself. Hooking an object into the message loop of a window is quite tricky, but fortunately, the necessary code has already materialized in some MFC classes which can be obtained on this site. (see article CHookWnd v1.02 by PJ Naughter). For this utility, I used Paul DiLascias classes, which served as starting point for PJ Naughter's extensions. They are implemented in the files Subclass.h and Subclass.cpp.

    Customization

    You can edit the constructor of the class CMainWndPlacement to change the section and entry where the main window size is stored. Currently "Settings" is used as section and "MainWndPos" as entry.

    Implementation Details

    CMainWndPlacement is derived from CSubclassWnd which provides the method HookWindow to hook your object to a given window and the virtual method WindowProc which is called for every message which arrives at the hooked window. In WindowProc you can just listen to all the arriving messages, you can filter them out or you can even generate your own messages. CMainWndPlacement itself only listens to the messages WM_PAINT and WM_DESTROY.

    One interesting implementation detail concerns the fact, that the class CMainWndPlacement has only one public method, namely InitialShow, even the constructor of CMainWndPlacement is private. Because of that, one can not allocate a CMainWndPlacement object on the stack or make it a member variable of another class. Generally such a technique restricts the flexibility of a class severely and should only be used for special cases (like this one).

    Remarks

    Just imagine what else can be done with a class like CSubclassWnd. Always consider the use of a window hook, when you want to add functionality to existing windows and when these windows belong to different C++ classes.

    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
    Switzerland Switzerland
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralHidden Regitry Data Value Pin
    sbuonocore19-Jul-05 3:31
    sbuonocore19-Jul-05 3:31 
    GeneralRe: Hidden Regitry Data Value Pin
    Martin.Holzherr19-Jul-05 4:15
    Martin.Holzherr19-Jul-05 4:15 
    GeneralMinimized on startup Pin
    Mark Donkers23-Oct-02 7:45
    Mark Donkers23-Oct-02 7:45 
    GeneralRe: Minimized on startup Pin
    sdfarmer3-Apr-05 9:02
    sdfarmer3-Apr-05 9:02 
    GeneralRe: Minimized on startup Pin
    Martin Holzherr3-Apr-05 20:27
    Martin Holzherr3-Apr-05 20:27 
    To be honest, I only tested it on Windows NT and Windows98.
    And there it works as expected.

    GeneralRe: Minimized on startup Pin
    ticica7-Jan-07 7:06
    ticica7-Jan-07 7:06 
    GeneralRemarks Pin
    merlin30-Oct-00 7:01
    merlin30-Oct-00 7:01 
    GeneralMDI app.. restoring windows plecements Pin
    meir18-Sep-00 21:42
    meir18-Sep-00 21:42 
    GeneralMDI app.. restoring windows plecements Pin
    meir18-Sep-00 21:41
    meir18-Sep-00 21:41 

    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.