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

效果图:

相关推荐
Jackson@ML14 分钟前
2026最新版Visual Studio安装使用指南
ide·c#·visual studio
莫生灬灬2 小时前
VueMultiBrowser 5.0 开源 - 基于 Vue3 + CEF 的多浏览器管理器
chrome·开源·c#·自动化·多开·cef3
xb11323 小时前
C#异步编程入门概念及核心概念
开发语言·c#
gc_22995 小时前
学习C#调用OpenXml操作word文档的基本用法(19:学习文档页脚类)
c#·word·openxml·页脚·footerpart
baivfhpwxf20236 小时前
C# Task.Run 和 Task.Factory.StartNew 的区别
开发语言·c#
数据的世界017 小时前
C# 封装、继承、多态 通俗解释
c#
人工智能AI技术8 小时前
【C#程序员入门AI】AI应用的操作系统:Semantic Kernel 2026实战
人工智能·c#
m5655bj8 小时前
Python 将 Markdown 转换为 Word 文档
开发语言·c#
黑棠会长9 小时前
ABP框架04.复杂业务关系实现(DDD实战)
数据库·c#·.net·ddd·abp