C# winform自定义Label控件使其能设置行距

该博客介绍了如何在 C# 的 Winform 应用中创建一个自定义 Label 控件,使其能够支持设置文本的行间距。通过重写 OnPaint 方法并利用 Graphics 对象进行测量和绘制,实现了文本的换行和行间距调整功能。博客内容包括控件的初始化、属性设置、以及行间距的计算和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Drawing;  
  6. using System.ComponentModel;  
  7. namespace WindowsFormsApplication10  
  8. {  
  9.     public partial class LabelTx : System.Windows.Forms.Label  
  10.     {  
  11.         int lineDistance = 5;//行间距      
  12.         Graphics gcs;  
  13.         int iHeight = 0, height = 200;  
  14.         string[] nrLine;  
  15.         string[] nrLinePos;  
  16.         int searchPos = 0;  
  17.         int section = 1;  
  18.         public int LineDistance  
  19.         {  
  20.             get { return lineDistance; }  
  21.             set  
  22.             {  
  23.                 lineDistance = value;  
  24.                 Changed(this.Font, this.Width, this.Text);  
  25.             }  
  26.         }  
  27.         public LabelTx()  
  28.             : base()  
  29.         {  
  30.             //this.TextChanged += new EventHandler(LabelTx_TextChanged);     
  31.             this.SizeChanged += new EventHandler(LabelTx_SizeChanged);  
  32.             this.FontChanged += new EventHandler(LabelTx_FontChanged);  
  33.             //this.Font = new Font(this.Font.FontFamily, this.Font.Size, GraphicsUnit.Pixel);    
  34.         }  
  35.         void LabelTx_FontChanged(object sender, EventArgs e)  
  36.         {  
  37.             Changed(this.Font, this.Width, this.Text);  
  38.         }  
  39.         void LabelTx_SizeChanged(object sender, EventArgs e)  
  40.         {  
  41.             Changed(this.Font, this.Width, this.Text);  
  42.         }  
  43.         public LabelTx(IContainer container)  
  44.         {  
  45.             container.Add(this);  
  46.             //base.Height     
  47.             //InitializeComponent();     
  48.         }  
  49.         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值