• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1449
  • Last Modified:

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?
0
maknight
Asked:
maknight
  • 3
  • 3
1 Solution
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now