CDocument::SetTitle() Problems

I have a Split Frame Window in my MSVC++ 6 application. In my OnNewDocument() override, I am calling the SetTitle() function. At any point past that I can call GetTitle() and it will return the correct title that I set, but the title never does appear in the title bar of the frame window. It just displays the program name. Why is this, and how can I display the text I want in the title bar?
maknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveGTRCommented:
I override the CDocument::SetTitle function and build my own title. Then pass on the title to CDocument.
0
GlennDeanCommented:
When I try calling SetTitle in OnNewDocument it displays the title followed by the program name.  The program name is stored in
CDocTemplate::m_strDocStrings,
but it is a protected data member so the following code in OnNewDocument doesn't work:
CDocTemplate * pDT=GetDocTemplate();
pDT->m_strDocStrings[0]="";  //Wipe out program name
SetTitle("ZZTop");
0
SteveGTRCommented:
To do this you could override your main frame (MAINFRM.CPP) classes OnUpdateFrameTitle(). Duplicate and specialize the process done in MFC to handle your special needs. Here is an example for an MDI application:

// Header file in class

  virtual void OnUpdateFrameTitle(BOOL bAddToTitle);
  void UpdateFrameTitleForDocument(LPCTSTR lpszDocName);

// Source code file

#include "afxpriv.h"
#ifndef _AFX_NO_OLE_SUPPORT
#include "..\src\oleimpl2.h"
#endif

void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
  if ((GetStyle() & FWS_ADDTOTITLE) == 0)
    return;     // leave it alone!

#ifndef _AFX_NO_OLE_SUPPORT
  // allow hook to set the title (used for OLE support)
  if (m_pNotifyHook != NULL && m_pNotifyHook->OnUpdateFrameTitle())
    return;
#endif

  CMDIChildWnd* pActiveChild = NULL;
  CDocument* pDocument = GetActiveDocument();

  if (bAddToTitle &&
      (pActiveChild = MDIGetActive()) != NULL &&
      (pActiveChild->GetStyle() & WS_MAXIMIZE) == 0 &&
      (pDocument != NULL ||
       (pDocument = pActiveChild->GetActiveDocument()) != NULL))
    UpdateFrameTitleForDocument(pDocument->GetTitle());
  else
    {
    LPCTSTR lpstrTitle = NULL;
    CString strTitle;

    if (pActiveChild != NULL)
      {
      strTitle = pActiveChild->GetTitle();

      if (!strTitle.IsEmpty())
        lpstrTitle = strTitle;
      }

    UpdateFrameTitleForDocument(lpstrTitle);
    }
}

void CMainFrame::UpdateFrameTitleForDocument(LPCTSTR lpszDocName)
{
  // copy first part of title loaded at time of frame creation
  TCHAR szText[256+_MAX_PATH];

  if (lpszDocName == NULL)
    lstrcpy(szText, m_strTitle);
  else if (GetStyle() & FWS_PREFIXTITLE)
    {
    szText[0] = '\0';   // start with nothing

    // get name of currently active view
    lstrcpy(szText, lpszDocName);
    // add current window # if needed
    if (m_nWindow > 0)
      wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);
    }
  else
    {
    // get name of currently active view
    lstrcpy(szText, lpszDocName);
    // add current window # if needed
    if (m_nWindow > 0)
      wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);
    }

  // set title if changed, but don't remove completely
  // Note: will be excessive for MDI Frame with maximized child
  AfxSetWindowText(m_hWnd, szText);
}

Good Luck,
Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

GlennDeanCommented:

   I didn't really understand all of Steve's code (which certainly seems to solve the problem), but I slimmed down his code to only overriding CMainFrame::OnUpdateFrameTitle and CDocument::OnNewDocument as follows:

OnUpdateFrameTitle(BOOL bAddIt)
{
 CDocument * pDoc = GetActiveDocument();
 CString newTitle = pDoc->GetTitle();
 CWnd * pWnd = FromHandle(m_hWnd);
 pWnd->SetWindowText(newTitle.GetBuffer(0));
}
OnNewDocument()
{"Call base class OnNewDocument()"
 SetTitle(ZZTop);
}
and it works great.  I'm sure I'm oversimplifying things, but ....
0
SteveGTRCommented:
The code I posted was slightly (very slightly) modified MFC code --- WINFRM.CPP and WINMDI.CPP.

GlennDean has simplified the code way down. My only concern is what happens when there is no active document? pDoc will not be valued and you'll throw an exception.

0
maknightAuthor Commented:
Boy. You have really have given me a problem! SteveGTR's answer definitely works and solves the problem, but b/c my application can never have no active document, I used GlennDean's example code in one project. I learned some nice tricks by looking through both code samples. I have accepted SteveGTR's answer b/c I also used his example in another project I'm starting. But, is there any way I can get GlennDean the points as well? I'm willing to part with another 100 points since I actually used that example as well.
0
GlennDeanCommented:
maknight:
  Thanks for the offer but there is an on going thing at the site about trying to split points.  It appears EE doesn't want to try it.  I also think Steve deserves the points more than I do since I provided my solution only after seeing Steve's code.  
   Glenn
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.