Static text to display long filenames with ellipses

The CStaticFilespec control is a lightweight class suitable for displaying long filespecs that may need to be truncated or compressed, in order to fit the size of the display. Here's and example of what the control looks like when used in a dialog box.

CStaticFilespec example

By default, CStaticFilespec supports 2 drawing styles: DT_END_ELLIPSIS and DT_PATH_ELLIPSIS. The former displays a truncated filespec with trailing ellipses while the latter adds embedded ellipses to a compressed filespec.

Using CStaticFilespec in a dialog is quite simple:
  1. Include CStaticFilespec.h in your dialog class' header file.

  2. Add member variables of type CStaticFilespec for every static text control you want to subclass.

  3. Subclass the static text controls in your dialog's OnInitDialog() method and set the controls' style.

      m_wndStaticFileEnd.SubclassDlgItem (IDC_FILE_END, this);
      m_wndStaticFilePath.SubclassDlgItem (IDC_FILE_PATH, this);
      m_wndStaticFilePath.SetPath (TRUE);

You can also use any other DT_ drawing style you like. For example, to draw right justified text that is vertically centered and has trailing ellipses, do:

  m_wndStaticText.SubclassDlgItem (IDC_STATIC_TEXT, this);
  m_wndStaticText.SetFormat (DT_RIGHT | DT_VCENTER);
  m_wndStaticText.SetWindowText ("d:\\There\\Is\\Always\\One\\More\\Bug.cpp");

Download source files (3 K) CStaticFilespec.cpp, CStaticFilespec.h
Download example project (14 K) (includes sources)



Comments

  • Don't Do This (editorial rant)

    Posted by Legacy on 11/01/2002 12:00am

    Originally posted by: Steve

    Instead of adding an ellipsis into you path, find another way to present the path. Ask yourself: why are you bothering to show the entire filepath if you aren't prepared to show the entire filepath? I would think that any user of VC6.0 would know the misery of being presented filepaths rendered useless through the use of an ellisis.

    Reply
  • Static Control text wrapping

    Posted by Legacy on 08/20/2002 12:00am

    Originally posted by: Geetha Sekar

    Suppose if the Static Control contains a single word which exceeds the width of the static control , Wordwrap doesn't take effect. If a space is provided it does a wordwrap.
    Could anyone give me a solution wrap singleword that exceeds the width of the static control.

    Reply
  • One interesting observation

    Posted by Legacy on 07/09/1999 12:00am

    Originally posted by: Winson Yung

    Hi there, I have noticed a very intesting behaviour when I run the sample code. Here is a segment of code I slightly modified just so that it will be easy to see on the screen:

    In StaticFilespecExampleDlg.cpp.....close to end of the file

    void CStaticFilespecExampleDlg::OnBtnUpdate()
    {
    CString strText; // text to be displayed

    GetDlgItemText (IDC_EDIT_TEXT, strText);
    SetDlgItemText (IDC_FILE_END, strText);
    Sleep(4000); <==========This is what I modified
    SetDlgItemText (IDC_FILE_PATH, strText);
    }

    you will notice that the long string you entered will not be
    shortened immediately, instead it gets truncated first, and
    then shortened. I couldn't figure out why it is doing that,
    and I need this to be fixed so that I can used in my program.

    Thanks,

    /Winson.

    Reply
  • Why don't you use standart styles SS_ENDELLIPSIS and SS_PATHELLIPSIS?

    Posted by Legacy on 04/08/1999 12:00am

    Originally posted by: Kir

    Job's already done for you by MS guys

    Reply
  • Thanks - this was exactly what I was looking for.

    Posted by Legacy on 04/01/1999 12:00am

    Originally posted by:

    Great class. Just "plugged and played".

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds