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;
                    }
                }
            }
        }

效果图:

相关推荐
心平气和量大福大3 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
-银雾鸢尾-7 小时前
C#中HashTable相关方法
开发语言·c#
geovindu11 小时前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
吴可可12311 小时前
C# CAD二次开发中如何退出窗口
c#
影寂ldy11 小时前
C# WinForm 三种自定义控件 + 完全自定义重绘控件知识点
开发语言·c#
ellis197011 小时前
C#异常相关关键字:Exceptions,throw,try,catch,finally
unity·c#
-银雾鸢尾-11 小时前
C#中的Dictionary相关方法
开发语言·c#
lzhdim12 小时前
C# 中读取CPU、硬盘和内存温度
开发语言·c#
WarPigs13 小时前
.NET项目app.Config笔记
c#·.net