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

效果图:

相关推荐
★YUI★6 小时前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
谷宇.6 小时前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程
LZQqqqqo7 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
Dm_dotnet9 小时前
Stylet启动机制详解:从Bootstrap到View显示
c#
三千道应用题11 小时前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
唐青枫15 小时前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
我好喜欢你~1 天前
C#---StopWatch类
开发语言·c#
一阵没来由的风1 天前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
一枚小小程序员哈1 天前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM1 天前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word