. Home Feedback Contents Search

Window 

Back Up

Window

Get Rid of ‘Untitled’ in the Window Caption

When a document has no current name, it is the default behavior of an application is to indicate that to the user by appending ‘Untitled’ to the caption bar. When a lack of a document name is the normal case for an application then the ‘Untitled’ nomenclature is inappropriate. Fortunately, there is a way to tell Windows to not do this. Before the main window is created, the application is given a chance to change the style settings that will be used with the window. One of those style bits is associated with appending the document name to the caption. If that particular style bit is removed then the document name won’t be appended to the application caption.

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    cs.style &= ~(LONG) FWS_ADDTOTITLE;
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;

    return TRUE;
    }

Save and restore

One really nice thing a user likes to see is for an application to start up in the same location it last ran in. This can be accomplished by reading the application’s positional data on shut down and saving that data. The next time the application starts up, it checks to see if it has any position data saved in the registry. If so, the previous position is reasserted and the application picks up where it last left off.

In the app's H

extern bool   g_bSizeAsserted;

In the app's cpp

bool   g_bSizeAsserted = false;

In MainFrm.cpp

 In the OnCreate()

     // Move the window to its last known size.

    UINT nBytes = 0;
    unsigned char* pBytes = 0;
    AfxGetApp()->GetProfileBinary(_T("Settings"), _T("WindowPlacement"), &pBytes, &nBytes);
    if (nBytes == sizeof(WINDOWPLACEMENT))
        {
        SetWindowPlacement((WINDOWPLACEMENT*) pBytes);
        g_bSizeAsserted = true;
        }
    if (pBytes && nBytes)
        delete[] pBytes;

In the OnClose()

void CMainFrame::OnClose()
    {

    // Save the application's position before we close down.

    WINDOWPLACEMENT wp;
    wp.length = sizeof(wp);
    GetWindowPlacement(&wp);
    AfxGetApp()->WriteProfileBinary(_T("Settings"),
                                    _T("WindowPlacement"),
                                    (BYTE*)&wp,
                                    sizeof(WINDOWPLACEMENT));
   
    CFrameWnd::OnClose();
    }

In the form view:

void CMyView::OnInitialUpdate()
    {
    CFormView::OnInitialUpdate();
   
    // If we haven't already asserted a size back in the MainFrame's create
    // then generate a size based upon the default size of the form's dialog layout.
    // Resize the parent window to be exactly the size required in order
    // to contain the view's dialog box like layout.

    if ( !g_bSizeAsserted )
        {
        GetParentFrame()->RecalcLayout();
        ResizeParentToFit(TRUE);
        ResizeParentToFit(FALSE);
        }

 

Setting min/max size

Sometimes, it makes sense to allow the user to change the application’s window size but to limit that size to some minimal and maximum size. Lets take the example of a CFormView application where we want to limit the smallest size that the window can be.

There’s a windows message that is issued before a size or movement message. Intercept this message by letting the class wizard insert a handler in the main window for a WM_GETMINMAXINFO message.

Let the minimal size be determined by the smallest size that we want to let our dialog template based form view to display as. During the time that the view is initializing, get the size of the appliction’s window and use that width and height as the minimal size value. We have to do the entire application size, not just the client area.

void CMyView::OnInitialUpdate()
    {
    // Let Windows handle putting the controls in an initial position
    // and sizing the parent to exactly wrap the view.

    CFormView::OnInitialUpdate();
    GetParentFrame()->RecalcLayout();
    ResizeParentToFit( TRUE );    // Have to do both TRUE and FALSE so that the
    ResizeParentToFit( FALSE );    // framework shrinks or grows as needed.

    // Let the parent frame know what the min size is.

    CRect    cr;
    CMainFrame* pwnd = (CMainFrame*)AfxGetMainWnd();
    pwnd->GetWindowRect( cr );
    pwnd->m_ptMin = CPoint( cr.Width(), cr.Height() );
    pwnd->m_bSizeIsValid = true;

    // Etcetera….
}

 Windows will call into the application to get its min and max allowable sizes before any movement message is processed. However, we only want to handle these when we’ve established a valid minimal size so we have to add a conditional to detect when the minimal size value is valid.

 void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
    {
    CFrameWnd::OnGetMinMaxInfo(lpMMI);    // Fill everything with default values.

    if ( m_bSizeIsValid )    // Only handle if min has been established.
        {
        lpMMI->ptMinTrackSize = m_ptMin;
        }
    }

Back Up