none
Window title ellipsis

    Question

  • I have a window with SizeToContent set to Width.
    And ResizeMode is set to NoResize, because I don't want a user resizing it.

    Sometimes the Title I give the window is long enough such that the ellipsis ("...") is automatically used to abbreviate it.

    I want my window to be wide enough to avoid the ellipsis in the title.

    I could have my way if I could do any of the following:

    - Set some property in the window that makes it do what I want.

    - Get some boolean from the window telling me if the title has been abbreviated.

    - Get the actual title shown in the window, and I can compare it to the actual Title.

    - Reliably determine the desired width of the window base on the Title text.  I imagine there are a lot of variables here... system settings, font settings, icon shown?, minimize/maximize button shown, etc.

    Any suggestions?
    Thanks
    Ion
    Tuesday, May 12, 2009 7:00 PM

Answers

  • Neither of those suggestions is a foolproof way of doing this. You'll have to use the system apis for measuring text (windows forms exposes them using the TextRenderer class) and translate its width to the WPF coordinate system, which will take account of DPI. All the info you need to complete the former is in MSDN, and for the latter there are a few examples of converting between WPF and Win32 coordinates out there.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Marked as answer by Tao Liang Friday, May 15, 2009 2:06 AM
    Wednesday, May 13, 2009 1:50 AM

All replies

  • A quick and dirty way that we thought of is to create a hidden TextBlock in your window that contains the window title text. Apply a margin on the left and right to account for the icon and the minimize/close buttons. The TextBlock will not size smaller than the text, and the window, being size-to-content, will not size smaller than the TextBlock.

    Ben
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, May 12, 2009 8:29 PM
  • I think you can use FormattedText's Width property to find out the exact width of the original (non-abbreviated) text, then you know the width of the window, so you can find out if the title text is abbreviated by checking if WindowWidth - TextWidth - Tolerance = 0. Adjust the Tolerance value to take into consideration of the icon, min/max buttons, etc.

    Re: I want my window to be wide enough to avoid the ellipsis in the title.
    Use the above technique in the windows's Loaded event, and update the window's width if necessary.

    Re: Set some property in the window that makes it do what I want.
    Not sure what do you mean.

    Re: Get some boolean from the window telling me if the title has been abbreviated.
    Subscribes to the window's SizeChanged event.

    Re: Get the actual title shown in the window, and I can compare it to the actual Title.
    I am not sure the best/easist way of doing this. All I can think of is to, start from the end, subtract each character from the original (non-abbreviated) string, calculate the actual width (using the FormattedText) of the subtracted string plus "...", and see if the calculated width is slightly less than or equal to the actual width of the abbreviated string (WindowWidth - Tolerance) shown in the window.

    Re: Reliably determine the desired width of the window base on the Title text.  I imagine there are a lot of variables here... system settings, font settings, icon shown?, minimize/maximize button shown, etc.
    I am also interested in knowing the "reliable way", as the above method relies on the Tolerance value, which could be different from OS to OS, or theme to theme.


    HTH,
    Mark
    Tuesday, May 12, 2009 10:39 PM
  • Neither of those suggestions is a foolproof way of doing this. You'll have to use the system apis for measuring text (windows forms exposes them using the TextRenderer class) and translate its width to the WPF coordinate system, which will take account of DPI. All the info you need to complete the former is in MSDN, and for the latter there are a few examples of converting between WPF and Win32 coordinates out there.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Marked as answer by Tao Liang Friday, May 15, 2009 2:06 AM
    Wednesday, May 13, 2009 1:50 AM
  • Thanks guys.  Using SystemParametersInfo(SPI_GETNONCLIENTMETRICS....) to get the caption font and TextRenderer.MeasureText works well for me to determine the necessary space for the title.

    Ion
    Monday, May 18, 2009 12:25 PM