Re: how to display full path instead of filename

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 12/31/04

  • Next message: Joseph M. Newcomer: "Re: Menu Not Available from Keyboard"
    Date: Fri, 31 Dec 2004 16:09:34 -0500
    
    

    Why do you need a TCHAR variable? This is an invitation to disaster. Learn to use CString
    for things like this!

    CString Text;
    if(pDocument == NULL)
         text = m_strTitle;
    else
       {
         CString title = pDocument->GetPathName();
          if(title.IsEmpty())
             { /* not saved yet */
               title = pDocument->GetTitle();
             } /* not saved yet */
          if(m_nWindow > 0)
            { /* has sequence number */
              CString n;
              n.Format(_T(".%d"), m_nWindow);
              title += n;
           } /* has sequence number */
        SetWindowText(title);
      }

    The general assumption should be as soon as you write a TCHAR array in MFC, particularly
    for purposes of forming a string, you have made a fundamental error in programming. Do
    not use TCHAR arrays unless there is no other way to do it. And I don't want to hear
    anything about "efficiency" coming into the discussion; the cost of activating a window is
    so high that the cost of the CString managment is unmeasurable by comparison.
                                    joe

    On Thu, 30 Dec 2004 21:13:32 +0100, "Václav Jedlicka" <no@spam.please> wrote:

    >Here is my solution:
    >override in CChildFrame:
    >
    >
    >//display full path instead of a filename only
    >void CChildFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
    >{
    > // update our parent window first
    > GetMDIFrame()->OnUpdateFrameTitle(bAddToTitle);
    >
    > if ((GetStyle() & FWS_ADDTOTITLE) == 0)
    > return; // leave child window alone!
    >
    > CDocument* pDocument = GetActiveDocument();
    > if (bAddToTitle)
    > {
    > TCHAR szText[256+_MAX_PATH];
    > if (pDocument == NULL)
    > lstrcpy(szText, m_strTitle);
    > else
    > {
    > CString ls_title = pDocument->GetPathName();
    > if(ls_title.GetLength() == 0){//not saved yet
    > ls_title = pDocument->GetTitle();
    > }
    > lstrcpy(szText, ls_title);
    > }
    > if (m_nWindow > 0)
    > wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);
    >
    > // set title if changed, but don't remove completely
    > SetWindowText(szText);
    > }
    >
    >
    >}
    >
    >I would prefer something more elegant but it works.
    >
    >Vaclav
    >
    >
    >
    >"Joseph M. Newcomer" <newcomer@flounder.com> píse v diskusním príspevku
    >news:e787t0dg97k3fv1c86reo599kp0ho4n3l7@4ax.com...
    >> When you create the window, remove the FWS_ADDTOTITLE style from the
    >window style,
    >> otherwise the MFC framework will keep replacing the value you
    >SetWindowText with.
    >> Typically you do this in PreCreateWindow, use &=~FWS_ADDTOTITLE on the cs
    >style flags.
    >> joe
    >> On Tue, 28 Dec 2004 20:11:03 -0800, "Naren"
    ><Naren@discussions.microsoft.com> wrote:
    >>
    >> >hi
    >> >first u get the full path of the file by using CFile::GetFilePath. it
    >> >returns the full path of the file. then update the window title bar by
    >using
    >> >SetWindowText
    >> >naren
    >> >
    >> >
    >> >"Václav Jedlièka" wrote:
    >> >
    >> >> Hello
    >> >>
    >> >> In a MDI app, how can I display full path of the doc in the titlebar of
    >the
    >> >> window?
    >> >>
    >> >> Thanks
    >> >>
    >> >> Vaclav
    >> >>
    >> >>
    >> >>
    >>
    >> Joseph M. Newcomer [MVP]
    >> email: newcomer@flounder.com
    >> Web: http://www.flounder.com
    >> MVP Tips: http://www.flounder.com/mvp_tips.htm
    >

    Joseph M. Newcomer [MVP]
    email: newcomer@flounder.com
    Web: http://www.flounder.com
    MVP Tips: http://www.flounder.com/mvp_tips.htm


  • Next message: Joseph M. Newcomer: "Re: Menu Not Available from Keyboard"

    Relevant Pages

    • Re: CString->LPCTSTR->LPWSTR?
      ... > und somit kompatibel zu dem LPCTSTR ... Was in der Funktion ankommt sieht gut aus. ... folgt der Microsoftkonvention für die Übergabe von CString an Funktionen. ... Da der Compiler auf _MBCS steht, müßte mir z.B. das TCHAR von CString ...
      (microsoft.public.de.vc)
    • Re: CString to const char*
      ... hostent* pHETIP = NULL; ... casting wchar_t* to char*, the result is often just wrong. ... but I thought CString was supposed to convert ... I know m CString object is the TCHAR based version as I recently ...
      (microsoft.public.vc.language)
    • Re: Dialog Application Error, eVC
      ... I wouldn't consider a CString to be inherently any safer than using TCHAR. ... my dislike of MFC and the issues it contains (as well as its work ...
      (microsoft.public.pocketpc.developer)
    • Re: CString to const char*
      ... very few of those require a C style cast. ... Most conversions in fact happen ... but I thought CString was supposed to convert to ... I know m CString object is the TCHAR based version as I recently ...
      (microsoft.public.vc.language)
    • Re: how to display full path instead of filename
      ... why did they use TCHAR? ... I took that code from MFC source, ... > CString Text; ...
      (microsoft.public.vc.mfc)