c# 文本超长截断
根据控件大小进行截断
private void SetStringTruncat(Static.LabelEx lbl) { string oldStr = lbl.Text; if (string.IsNullOrEmpty(oldStr)) return; Graphics g = lbl.CreateGraphics(); string strNewStr = string.Empty; for (int i = 0; i < oldStr.Length; i++) { strNewStr = oldStr.Substring(0, oldStr.Length - i) + "…"; SizeF sizeF = g.MeasureString(strNewStr, lbl.Font, 1000, StringFormat.GenericTypographic); if (sizeF.Width <= lbl.MaximumSize.Width - 20) { break; } } g.Dispose(); if (strNewStr == oldStr + "…") strNewStr = oldStr; lbl.Text = strNewStr; }
作者:冰封一夏
出处:http://www.cnblogs.com/bfyx/
HZHControls官网:http://www.hzhcontrols.cn
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git