0

我正在编写一个 Windows 窗体应用程序,该应用程序具有一个向用户显示状态信息的 StatusStrip,并在鼠标悬停在相关事物上时进行提示。然而,当程序在它的最小窗口大小时,文本有时会比整个 StatusStrip 大,标签就会消失。必须有一个解决方法,理想情况下,当文本大于窗口允许时,我希望它自动省略。但是怎么做?

提前感谢=)

4

2 回答 2

1

设置 TextAlign = MiddleLeft

设置弹簧 = 真

这样你不会得到椭圆,但它也不会消失。

如果你想要椭圆,你可能必须实际测量宽度,并相应地调整你的文本。这不是一件容易的事。

于 2009-09-05T02:01:59.973 回答
0

您的消息很旧,但由于我遇到了同样的问题,所以我在这里发布了我找到的解决方案。

我使用该实用程序类:

Imports System.Drawing
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Namespace AutoEllipsis
    Public Enum EllipsisFormat
        None = 0
        AtEnd = 1
        AtStart = 2
        AtMiddle = 3
        Path = 8
        Word = 16
    End Enum

    ''' <remarks>
    ''' Adapted from Auto Ellipsis project published by Thomas Polaert under The Code Project Open License (CPOL) 1.02
    ''' http://www.codeproject.com/Articles/37503/Auto-Ellipsis
    ''' </remarks>
    Public Class Ellipsis
        Public Shared ReadOnly EllipsisChars = "..."

        Private Shared PrevWord As Regex = New Regex("\W*\w*$")
        Private Shared NextWord As Regex = New Regex("\w*\W*")

        Private TargetWidth As Integer
        Private TargetFont As Font
        Private Ctrl As Control

        Private Sub New(ByRef Ctrl As Control)
            Me.Ctrl = Ctrl
        End Sub

        Private Sub New(ByVal MaxWidth As Integer, ByVal TargetFont As Font)
            Me.TargetWidth = MaxWidth
            Me.TargetFont = TargetFont
        End Sub

        Private ReadOnly Property Width() As Integer
            Get
                If Me.Ctrl IsNot Nothing Then
                    Return Me.Ctrl.Width
                Else
                    Return Me.TargetWidth
                End If
            End Get
        End Property

        Private ReadOnly Property MeasureText(ByVal Text As String) As Size
            Get
                If Me.Ctrl IsNot Nothing Then
                    Using Dc As Graphics = Ctrl.CreateGraphics()
                        Return TextRenderer.MeasureText(Dc, Text, Me.Ctrl.Font)
                    End Using
                Else
                    Return TextRenderer.MeasureText(Text, Me.TargetFont)
                End If
            End Get
        End Property

        Public Shared Function Compact(ByVal Text As String, ByVal MaxWidth As Integer, ByVal TargetFont As Font, ByVal Options As EllipsisFormat) As String
            If MaxWidth = Nothing Then
                Throw New ArgumentNullException("MaxWidth")
            End If
            If TargetFont Is Nothing Then
                Throw New ArgumentNullException("TargetFont")
            End If
            Return Ellipsis.Compact(Text, New Ellipsis(MaxWidth, TargetFont), Options)
        End Function

        Public Shared Function Compact(ByVal Text As String, ByRef Ctrl As Control, ByVal Options As EllipsisFormat) As String
            If Ctrl Is Nothing Then
                Throw New ArgumentNullException("Ctrl")
            End If
            Return Ellipsis.Compact(Text, New Ellipsis(Ctrl), Options)
        End Function

        Private Shared Function Compact(ByVal Text As String, Elp As Ellipsis, ByVal Options As EllipsisFormat) As String
            If String.IsNullOrEmpty(Text) Then
                Return Text
            End If

            If EllipsisFormat.AtMiddle & Options = 0 Then
                Return Text
            End If

            Dim TextSize As Size = Elp.MeasureText(Text)

            If TextSize.Width <= Elp.Width Then
                Return Text
            End If

            Dim Pre As String = ""
            Dim Mid As String = Text
            Dim Post As String = ""

            Dim IsPath As Boolean = (EllipsisFormat.Path & Options) <> 0

            If IsPath Then
                Pre = Path.GetPathRoot(Text)
                Mid = Path.GetDirectoryName(Text).Substring(Pre.Length)
                Post = Path.GetFileName(Text)
            End If

            Dim Len As Integer = 0
            Dim Seg As Integer = Mid.Length
            Dim Fit As String = ""

            While Seg > 1
                Seg -= Seg / 2

                Dim Left As Integer = Len + Seg
                Dim Right As Integer = Mid.Length

                If Left > Right Then
                    Continue While
                End If

                If EllipsisFormat.AtMiddle & Options = EllipsisFormat.AtMiddle Then
                    Left = Left / 2
                    Right = Right / 2
                ElseIf EllipsisFormat.AtStart & Options <> 0
                    Right -= Left
                    Left = 0
                End If

                If EllipsisFormat.Word & Options <> 0 Then
                    If EllipsisFormat.AtEnd & Options <> 0 Then
                        Left -= PrevWord.Match(Mid, 0, Left).Length
                    End If
                    If EllipsisFormat.AtStart & Options <> 0 Then
                        Right += NextWord.Match(Mid, Right).Length
                    End If
                End If

                Dim Tst As String = Mid.Substring(0, Left) + EllipsisChars + Mid.Substring(Right)

                If IsPath Then
                    Tst = Path.Combine(Path.Combine(Pre, Tst), Post)
                End If

                TextSize = Elp.MeasureText(Tst)

                If TextSize.Width <= Elp.Width Then
                    Len += Seg
                    Fit = Tst
                End If
            End While

            If Len = 0 Then
                If Not IsPath Then
                    Return EllipsisChars
                End If

                If Pre.Length = 0 And Mid.Length = 0 Then
                    Return Post
                End If

                Fit = Path.Combine(Path.Combine(Pre, EllipsisChars), Post)
                TextSize = Elp.MeasureText(Fit)

                If TextSize.Width > Elp.Width Then
                    Fit = Path.Combine(EllipsisChars, Post)
                End If
            End If

            Return Fit
        End Function
    End Class
End Namespace

用法(ToolStripStatusLabel):

Dim Lbl As New ToolStripStatusLabel()
Dim SomeText As String = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas"
Lbl.Text = Ellipsis.Compact(SomeText, Lbl.Width - Lbl.Padding.Horizontal, Lbl.Font, EllipsisFormat.AtMiddle & EllipsisFormat.Path)

用法(标签):

Dim Lbl As New Label()
Dim SomeText As String = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas"
Lbl.Text = Ellipsis.Compact(SomeText, Lbl, EllipsisFormat.AtMiddle & EllipsisFormat.Path)
于 2015-08-11T11:37:52.897 回答