C# 标签中的路径显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:06:24  来源:igfitidea点击:

Path Display in Label

提问by Nick

Are there any automatic methods for trimming a path string in .NET?

是否有任何自动方法可以在 .NET 中修剪路径字符串?

For example:

例如:

C:\Documents and Settings\nick\My Documents\Tests\demo data\demo data.emx

becomes

变成

C:\Documents...\demo data.emx

It would be particularly cool if this were built into the Label class, and I seem to recall it is--can't find it though!

如果将其内置到 Label 类中会特别酷,而且我似乎记得它是 - 虽然找不到它!

采纳答案by lubos hasko

Use TextRenderer.DrawTextwith TextFormatFlags.PathEllipsisflag

TextRenderer.DrawTextTextFormatFlags.PathEllipsis标志一起使用

void label_Paint(object sender, PaintEventArgs e)
{
  Label label = (Label)sender;
  TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.ClientRectangle, label.ForeColor, TextFormatFlags.PathEllipsis);
}

Your code is 95% there. The only problem is that the trimmed text is drawn on top of the text which is already on the label.

你的代码是 95% 那里。唯一的问题是修剪后的文本是在标签上已经存在的文本之上绘制的。

Yes thanks, I was aware of that. My intention was only to demonstrate use of DrawTextmethod. I didn't know whether you want to manually create event for each label or just override OnPaint()method in inherited label. Thanks for sharing your final solution though.

是的,谢谢,我知道这一点。我的目的只是为了演示DrawText方法的使用。我不知道您是要为每个标签手动创建事件还是只是OnPaint()在继承的标签中覆盖方法。感谢您分享您的最终解决方案。

回答by Darren Kopp

What you are thinking on the label is that it will put ... if it is longer than the width (not set to auto size), but that would be

你在标签上的想法是它会放......如果它比宽度长(未设置为自动大小),但那将是

c:\Documents and Settings\nick\My Doc...

If there is support, it would probably be on the Path class in System.IO

如果有支持,它可能会在 System.IO 中的 Path 类上

回答by denis phillips

You could use the System.IO.Path.GetFileName method and append that string to a shortened System.IO.Path.GetDirectoryName string.

您可以使用 System.IO.Path.GetFileName 方法并将该字符串附加到缩短的 System.IO.Path.GetDirectoryName 字符串。

回答by Quibblesome

Not hard to write yourself though:

不难写自己虽然:

    public static string TrimPath(string path)
    {
        int someArbitaryNumber = 10;
        string directory = Path.GetDirectoryName(path);
        string fileName = Path.GetFileName(path);
        if (directory.Length > someArbitaryNumber)
        {
            return String.Format(@"{0}...\{1}", 
                directory.Substring(0, someArbitaryNumber), fileName);
        }
        else
        {
            return path;
        }
    }

I guess you could even add it as an extension method.

我想您甚至可以将其添加为扩展方法。

回答by Nick

@ lubos haskoYour code is 95% there. The only problem is that the trimmed text is drawn on top of the text which is already on the label. This is easily solved:

@ lubos hasko你的代码是 95%。唯一的问题是修剪后的文本是在标签上已经存在的文本之上绘制的。这很容易解决:

    Label label = (Label)sender;
    using (SolidBrush b = new SolidBrush(label.BackColor))
        e.Graphics.FillRectangle(b, label.ClientRectangle);
    TextRenderer.DrawText(
        e.Graphics, 
        label.Text, 
        label.Font, 
        label.ClientRectangle, 
        label.ForeColor, 
        TextFormatFlags.PathEllipsis);