通过显示点而不是文件路径Vb.Net的某些部分,在一行中的标签中拟合完整的文件路径

时间:2014-09-24 22:46:46

标签: vb.net label

enter image description here

嗨,大家好,我已经为这件事寻找了不同的方法,但无法做到。使用AutoEllipses属性,也设置MaximumSize但无效。我如何获得一个标签来显示正在扫描的文件的名称,就像我附上的图片一样?我的意思是,标签应该从文件的完整路径的开头显示一些部分,然后是一些点,然后是带扩展名的文件名。

3 个答案:

答案 0 :(得分:2)

你可能会考虑一些事情;但是,这里的可能性范围太大而无法覆盖。

为了正确编码,您需要了解三(3)项内容:您要发送到标签的文件路径字符串的实际measured size;测量的标签尺寸;和文件名的长度(以字符为单位)。可能有一个奇特的功能,减少了你需要做和知道的事情的数量;但是,我不打算阅读大量的文档。

以上所有内容都需要是动态的,以便您的标签可以采用不同的String对象并正确呈现它们。

Dim filePath As String = ""
Dim FileDirectory As String = ""
Dim fileName As String = ""
Dim filePathLength As SizeF = 0.0
Dim labelLength As Double = 0.0
Dim fileNameLength As Integer = 0.0


' Come up with a way for measuring your string: 
Dim _GraphicsUnit As Graphics = Me.CreateGraphics()

' Receive your file path, here:
' and work with your file path-related Strings:
filePath = ' SOMETHING
fileDirectory = Path.GetDirectoryName(filePath)
fileName = Path.GetFileName(filePath)
fileNameLength = fileName.Length()

' Measure the length of you path
filePathLength = _GraphicsUnit.MeasureString(filePath, INSERTFONT) * _GraphicsUnit.Inches 'or other usable unit


If filePathLength > SIZEOFLABEL Then

    While filePathLength > SIZEOFLABEL

        ' Grab a substring of the the fileDirecory, append the "...", and keep measuring until shorter
        ' than SIZEOFLABEL.
        ' Your algorithm will need to figure out how and when to re-append the fileName

    End While

End If

以上是伪代码并且充满了错误。以上是展示.Net可以为您提供的一些工具的方法,即GraphicsUnit {{3} }和Path. stuff。这两个都有帮助。你将基本上处理这两个“事物”和SubString()方法。

我的尝试是向您展示如何开始考虑您面前的问题,以便您可以开始解决问题(因为正如上面的评论所述,没有太多可以做的事情你需要什么)。您的初始问题没有提供任何基于上述伪代码的原始代码;换句话说,我不想编写你的整个项目,但至少想要得到答案。

另外一个想法:.MaxLength

上述方法非常耗费内存 - 需要进行大量重复,而这些重复可能并非必要。只需知道大小 - 在本例中为MaxLength属性 - 可能会有所帮助。设置.MaxLength的{​​{1}}属性可以让您知道框中可以容纳多少个字符(您需要考虑其他一些元素,例如字体,大小等)。

知道这个数字,你可以完全避免循环:

TextBox的子串等于fileDirectory属性的长度,删除等于.MaxLength和“...”大小的字符数,并追加后两者。

答案 1 :(得分:1)

我在这里找到了这个问题的答案Shorten The File Path,就代码而言,这是一个非常简短的解决方案。

答案 2 :(得分:0)

您可以使用PathCompactPathExW pInvoke方法来完成此任务:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Program
    <DllImport("shlwapi.dll", EntryPoint:="PathCompactPathExW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Public Shared Function PathCompactPathEx(<MarshalAs(UnmanagedType.LPTStr)> pszOut As System.Text.StringBuilder, _
                <MarshalAs(UnmanagedType.LPTStr)> pszSrc As String, _
                cchMax As UInteger, _
                reserved As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Public Shared Sub Main()

        Dim longPath As String = "c:\a\very\very\long\path\that\needs\to\be\shortened\by\calling\the\PathCompactpathEx.ext"
        Dim length As Integer = 40

        Dim result As String = CompactPath(longPath, length)

        'Prints c:\a\very\very\...\PathCompactpathEx.ext
        Console.WriteLine(result)

        Console.ReadLine()
    End Sub

    Public Shared Function CompactPath(longPathName As String, wantedLength As Integer) As String
        'NOTE: You need to create the builder with the required capacity before calling function.
        'See http://msdn.microsoft.com/en-us/library/aa446536.aspx
        Dim sb As New StringBuilder(wantedLength + 1)
        PathCompactPathEx(sb, longPathName, CUInt(wantedLength + 1), 0)
        Return sb.ToString()
    End Function
End Class