Click here to Skip to main content
Click here to Skip to main content

Save/Restore main window size with a single function call

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

    About the Author

    Martin Holzherr

    Switzerland Switzerland
    No Biography provided

    Comments and Discussions

     
    GeneralHidden Regitry Data Value Pinmembersbuonocore19-Jul-05 3:31 
    GeneralRe: Hidden Regitry Data Value PinmemberMartin.Holzherr19-Jul-05 4:15 
    GeneralMinimized on startup PinmemberMark Donkers23-Oct-02 7:45 
    GeneralRe: Minimized on startup Pinmemberws_webking3-Apr-05 9:02 
    GeneralRe: Minimized on startup PinmemberMartin Holzherr3-Apr-05 20:27 
    GeneralRe: Minimized on startup Pinmemberticica7-Jan-07 7:06 
    GeneralRemarks PinsussMerlin30-Oct-00 7:01 
    GeneralMDI app.. restoring windows plecements Pinsussmeir18-Sep-00 21:42 
    GeneralMDI app.. restoring windows plecements Pinsussmeir18-Sep-00 21:41 

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

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

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