2009-09-05 3 views
0

사용자에게 상태 정보를 표시하는 StatusStrip이 있고 관련 항목 위에 마우스가있을 때 힌트를주는 Windows Forms App을 코딩하고 있습니다. 그러나 프로그램의 크기가 최소화되면 텍스트가 전체 StatusStrip보다 크기 때문에 레이블이 사라집니다. 이 문제를 해결할 수 있어야합니다. 텍스트가 창보다 큰 경우 자동 줄임표를 사용하고 싶습니다. 그러나 어떻게? 사전에StatusStrip 레이블에 AutoEllipsis가 있습니까?

감사합니다 =)

+0

그것은 vb.net 태그를 사용하기 충분

는 그 유틸리티 클래스를 사용합니다. 질문이 VB.NET에만 국한되지 않는 한 제목에 VB.NET을 사용하지 마십시오. –

+0

좋아요 ^^ 지금부터 태그를 사용하겠습니다. –

답변

1

설정 textAlign을 = MiddleLeft

설정 봄 = 진정한

당신은 타원이 방법을받지 않습니다,하지만 역시 사라지지 않습니다.

타원을 원한다면 너비를 실제로 측정해야하고 그에 따라 텍스트를 조정해야 할 수도 있습니다. 쉬운 일은 아닙니다.

+2

와우, 그건 꽤 완고합니다. 왜 AutoEllipsis 속성이 없는지 알고 싶습니다. :( –

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)