c# label 自定义行间距

label 添加 Paint 事件。用"\n" 段落换行

复制代码
     private void label2_Paint(object sender, PaintEventArgs e)
        {
            int LineDistance = 8;//行间距
            System.Windows.Forms.Label label = sender as System.Windows.Forms.Label;
            System.Drawing.Font drawFont = label.Font;
            label.AutoSize = false;
            SolidBrush drawBrush = new SolidBrush(label.ForeColor);
            Graphics g = e.Graphics;
            g.Clear(label.BackColor);
            StringFormat drawFormat = new StringFormat();    
            string[] arrDrawString = label.Text.Split(new char[] { '\n'});

            int height = 0;
            foreach (string str in arrDrawString)
            {
                //文本的矩形区域大小
                SizeF textSize = g.MeasureString(str, label.Font);

                //计算行数
                int strLineCount = Convert.ToInt32(Math.Ceiling(textSize.Width / label.Width));


                height += Convert.ToInt16((textSize.Height + LineDistance) * strLineCount);
            }

            label.Height = height;   //计算调整后的高度
            float netTextPos_Y = 0;   // 下一行的位置
            foreach (string drawString in arrDrawString)
            {
                bool drawText = false;
                int strLenght = 1;   // 长度
                int startIndex = 0;  // 开始位置
                for (int i = 0; i < drawString.Length; i++, strLenght++)
                {
                    string subN = drawString.Substring(startIndex, strLenght);
                    if (startIndex + strLenght >= drawString.Length)
                    {
                        drawText = true;
                    }
                    else
                    {
                        string subN1 = drawString.Substring(startIndex, strLenght + 1);
                        if (g.MeasureString(subN, label.Font).Width <= label.Width && g.MeasureString(subN1, label.Font).Width > label.Width)
                        {
                            drawText = true;
                        }
                    }
                    if (drawText)
                    {
                        drawText = false;
                        strLenght = 0;
                        startIndex = i + 1;
                        SizeF textSize = g.MeasureString(subN, label.Font);
                        e.Graphics.DrawString(subN, drawFont, drawBrush, 0, netTextPos_Y , drawFormat);
                        netTextPos_Y = netTextPos_Y + textSize.Height + LineDistance;
                    }
                }
            }
        }

效果图:

相关推荐
就是有点傻2 小时前
如何创建一个WebApi服务端
服务器·c#
她说彩礼65万3 小时前
C# params使用
开发语言·c#·log4j
专注VB编程开发20年3 小时前
C#内存加载dll和EXE是不是差不多,主要是EXE有入口点
数据库·windows·microsoft·c#
她说彩礼65万5 小时前
C# 反射
java·算法·c#
laocooon5238578865 小时前
C#二次开发中简单块的定义与应用
android·数据库·c#
YJlio5 小时前
BgInfo 学习笔记(11.5):多种输出方式(壁纸 / 剪贴板 / 文件)与“更新其他桌面”实战
笔记·学习·c#
Zhen (Evan) Wang5 小时前
.NET 6 API使用Serilog APM
c#·.net
wuk9987 小时前
C# 开发 FTP 客户端
开发语言·c#
咕白m6257 小时前
使用 C# 设置 Word 段落对齐样式
后端·c#
武藤一雄7 小时前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore