在标签路径显示 [英] Path Display in Label

查看:133
本文介绍了在标签路径显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何自动方法在.NET微调路径字符串?

例如:

  C:\ Documents和Settings \尼克\我的文档\测试\演示数据\演示data.emx
 

变为

  C:\文件... \演示data.emx
 

这将是特别凉爽,如果这被内置到标签类,我似乎记得这是 - !不容觉得它虽然

解决方案

使用 TextRenderer.DrawText TextFormatFlags.PathEllipsis 标志

 无效label_Paint(对象发件人,PaintEventArgs的E)
{
  标签标签=(标签)发送;
  TextRenderer.DrawText(e.Graphics,label.Text,label.Font,label.ClientRectangle,label.ForeColor,TextFormatFlags.PathEllipsis);
}
 

  

您code为95%存在。唯一   问题是,修整文本是   绘制在其上是文本的顶部   已在标签上。

是的,谢谢,我意识到了这一点。我的目的只是为了演示如何使用 DrawText的方法。我不知道你是否要手动创建活动为每个标签或只覆盖继承的标签的OnPaint()方法。感谢您的分享,虽然最终的解决方案。

Are there any automatic methods for trimming a path string in .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!

解决方案

Use TextRenderer.DrawText with TextFormatFlags.PathEllipsis flag

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.

Yes thanks, I was aware of that. My intention was only to demonstrate use of DrawText method. 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.

这篇关于在标签路径显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆