StatusStrip 标签中的自动省略?

发布于 2024-08-04 03:07:16 字数 185 浏览 9 评论 0原文

我正在编写一个 Windows 窗体应用程序,它有一个 StatusStrip,可以向用户显示状态信息,并在鼠标悬停在相关内容上时提供提示。但是,当程序处于最小窗口大小时,文本有时比整个 StatusStrip 还要大,并且标签会消失。必须有一个解决方法,理想情况下,当文本大于窗口允许的大小时,我希望它自动省略。但如何呢?

预先感谢 =)

I'm coding a Windows Forms App that has a StatusStrip that displays status informations to the user, and hints when the mouse is over relevant things. However, when the program is on it's minimal window size, the text sometimes is bigger than the whole StatusStrip, and the label simply dissapears. There must be a workaround to this, ideally I'd like it to auto ellipsis when the text is bigger than the window allows. But how?

Thanks in advance =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2024-08-11 03:07:16

Set TextAlign = MiddleLeft

Set Spring = True

这样你不会得到省略号,但它也不会消失。

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

Set TextAlign = MiddleLeft

Set Spring = True

You won't get ellipses this way, but it won't disappear either.

If you want ellipses you may have to actually measure the width, and adjust your text accordingly. Not an easy task.

忘东忘西忘不掉你 2024-08-11 03:07:16

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

我使用该实用程序类:

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)

Your message is old, but as I had the same problem, I post here the solution I found.

I use that utility class :

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

Usage (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)

Usage (Label) :

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文