The Code Project View our sponsorsClick here for Dundas Consulting - experts in MFC, C++, TCP/IP and ASPAdvertise on the CodeProject
Home >> C++ / MFC / STL >> Unedited Reader Contributions

Saving a windows size position and state in MFC
By James Pullicino

Saving the size and positions of windows so that they can be restored next time the app loads up 
 Beginner
 VC5, VC6, VC7, Win95, Win98, NT4, W2K, MFC
 Posted 31 May 2001
Articles by this author
Send to a friend
Printer friendly version
[This is an unedited reader contribution] [Modify this article] [Delete this article]
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
7 users have rated this article. result:
3 out of 5.

Introduction

When building a UI application, one feature which I always include is saving the size and positions of my windows so that they can be restored next time the app loads up. Each time I make a new application, I always try a new method of doing this. Finally I've come up with what I think is the simplest and best solution to save and restore windows sizes and positions.

Get/SetWindowPlacement()

The trick lies behind the Get/SetWindowPlacement functions. The GetWindowPlacement function is part of the Windows API and it basically retrieves the show state and the restored, minimized, and maximized positions a window and stores it into a WINDOWPLACEMENT structure. This structure can then be passed to the SetWindowPlacement function to restore the window to its old state.

Saving the window placement

Saving the WINDOWPLACEMENT structure is fairly simple and can be done from anywhere in your program. Since I use MFC for my UI applications I found that the best place to save the window's size and position is in the DestroyWindow virtual function, since it is called every time the window closes, for every type of window:

BOOL CMainFrame::DestroyWindow() 
{
    WINDOWPLACEMENT wp;
    GetWindowPlacement(&wp);
    AfxGetApp()->WriteProfileBinary("MainFrame", "WP", (LPBYTE)&wp, sizeof(wp));

    return CMDIFrameWnd::DestroyWindow();
}

The WriteProfileBinary() is part of the MFC class CWinApp which simply dumps the WINDOWPLACEMENT structure into the registry as a REG_BINARY value called "WP" in a key called "MainFrame". A good idea, especially for child windows of the CMainFrame class, is to replace the hardcoded "MainFrame" with the caption of the frame.

Restoring the window placement

Next, we need to restore the window. The SetWindowPlacement function will only work after the window has been created, so the best place to call it is when the window is about to be shown for the first time. To my knowledge there is no single windows message or overridable virtual function to serve this purpose. However it is possible to handle the WM_SHOWWINDOW message and call SetWindowPlacement from there. Ideally it will be as simple as this:

void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) 
{
    CMDIFrameWnd::OnShowWindow(bShow, nStatus);

    if(bShow && !IsWindowVisible())
    {
        WINDOWPLACEMENT *lwp;
        UINT nl;

        if(AfxGetApp()->GetProfileBinary("MainFrame", "WP", (LPBYTE*)&lwp, &nl))
        {
            SetWindowPlacement(lwp);
            delete [] lwp;
        }
    }
}

The formal parameter bShow is TRUE when the window is about to be shown, and false when it is about to be hidden. With the if() statement we are ensuring that we will reposition the window only when the window is hidden and about to be shown. There are two problems here:

1) If your application hides the window and shows it during program execution, it will be displayed at the saved coordinates each time.

2) SetWindowPlacement causes a WM_SHOWWINDOW to be fired.

A simple workaround fixes the problem:

void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) 
{
    CMDIFrameWnd::OnShowWindow(bShow, nStatus);

    static bool bOnce = true;

    if(bShow && !IsWindowVisible()
    && bOnce)
    {
        bOnce = false;

        WINDOWPLACEMENT *lwp;
        UINT nl;

        if(AfxGetApp()->GetProfileBinary("MainFrame", "WP", (LPBYTE*)&lwp, &nl))
        {
            SetWindowPlacement(lwp);
            delete [] lwp;
        }
    }
}

A static variable inside a function is only initialized once. Since the if() statement now requires bOnce to be true in order to execute the block, we are assured that the code will be only executed once; the first time the window is shown. Instead of having bOnce as a static it could obviously also be a member variable. In fact, if you create two instances of the same class, bOnce will only be true once, which means that you'll have to create it as a member variable.

Happy Coding!

[Top] Sign in to vote for this article:     PoorExcellent  
Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
 Keyword Filter
 View   Per page   Messages since
New threadMessages 1 to 6 of 6 (Total: 6)First Prev Next Last
Subject 
Author 
Date 
  Same Old Problem...
James R. Twine 16:33 7 Jun 01 
  What about min/maximize
marcus78 13:21 4 Jun 01 
  Re: What about min/maximize
Scot Brennecke 5:05 5 Jun 01 
  Re: What about min/maximize
James Pullicino 5:15 5 Jun 01 
  Re: What about min/maximize
Michal Kowalski 3:11 7 Jun 01 
  Re: What about min/maximize
James Pullicino 3:18 8 Jun 01 
Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

Home >> C++ / MFC / STL >> Unedited Reader Contributions
last updated 31 May 2001
Article content copyright James Pullicino, 2001
everything else © CodeProject, 1999-2001.
The Code Project View our sponsorsGet your new domain name now!Advertise on the CodeProject