Click here to Skip to main content
13,197,804 members (40,439 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 13 Sep 2000

Save/Restore main window size with a single function call

, 13 Sep 2000
Rate this:
Please Sign up or sign in to vote.
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.


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


    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.


    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

    Martin Holzherr
    Switzerland Switzerland
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralHidden Regitry Data Value Pin
    sbuonocore19-Jul-05 3:31
    membersbuonocore19-Jul-05 3:31 
    Hi and congratulation for your code!
    I'd liek to know if it's possible (and how to do it) to write into the registry a 'masked Value Data'.I'd like infact to save in the registry a password but of course I don't want someone else to see it cleary!
    is ti possible to show it to the registry as an...asterisk array (*****) or to hide it?!?!
    Thanks a lot.

    GeneralRe: Hidden Regitry Data Value Pin
    Martin.Holzherr19-Jul-05 4:15
    memberMartin.Holzherr19-Jul-05 4:15 
    GeneralMinimized on startup Pin
    Mark Donkers23-Oct-02 7:45
    memberMark Donkers23-Oct-02 7:45 
    GeneralRe: Minimized on startup Pin
    ws_webking3-Apr-05 9:02
    memberws_webking3-Apr-05 9:02 
    GeneralRe: Minimized on startup Pin
    Martin Holzherr3-Apr-05 20:27
    memberMartin Holzherr3-Apr-05 20:27 
    GeneralRe: Minimized on startup Pin
    ticica7-Jan-07 7:06
    memberticica7-Jan-07 7:06 
    GeneralRemarks Pin
    Merlin30-Oct-00 7:01
    sussMerlin30-Oct-00 7:01 
    GeneralMDI app.. restoring windows plecements Pin
    meir18-Sep-00 21:42
    sussmeir18-Sep-00 21:42 
    GeneralMDI app.. restoring windows plecements Pin
    meir18-Sep-00 21:41
    sussmeir18-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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.171020.1 | Last Updated 14 Sep 2000
    Article Copyright 2000 by Martin Holzherr
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid