这是一个自动高度控制。如果更改控件的宽度,控件的高度也会发生变化,从而可以显示整个文本。
您可以使用不同的方法创建此类控件,包括:
方法 1:自动调整托管标签的复合控件的大小
此方法基于托管自动调整大小Label
在自动调整大小时具有动态最大宽度Control
。在这种方法中,我们根据控件的宽度设置标签的最大宽度,并且由于标签是自动调整大小的,因此它的高度将自动设置为显示所有文本,然后我们根据标签的高度设置控件的高度。
方法 2:从头开始自动调整大小,无需标签的简单控制
该方法基于重写SetBoundsCore并根据其设置控件的大小Text
尺寸。在这种方法中,我们根据控件的宽度计算文本的大小,使用TextRenderer.MeasureText然后将计算出的高度设置为控件的高度。在这种方法中,您应该自己处理文本格式标志和渲染。
在这两种方法中ControlDesigner
用于禁用除左和右之外的所有尺寸的手柄。
请注意
这些不是唯一可用的方法,但却是很好的例子。作为另一种选择,您可以继承Label
并改变它的行为。
方法 1:自动调整托管标签的复合控件的大小
它根据设置工作AutoSize
标签的属性为true
然后设置MaximumSize
基于控制的标签Width
。控件的高度也是根据标签的高度设置的。
您可以简单地绘制图像OnPaint
方法。您还可以添加一个PictureBox
对于图像。
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
[Designer(typeof(MyLabelDesigner))]
public partial class MyLabel : Control
{
public MyLabel() { InitializeComponent(); }
private System.Windows.Forms.Label textLabel;
private void InitializeComponent()
{
this.textLabel = new System.Windows.Forms.Label();
this.textLabel.AutoSize = true;
this.textLabel.Location = new System.Drawing.Point(0, 0);
this.textLabel.Name = "label1";
textLabel.SizeChanged += new EventHandler(textLabel_SizeChanged);
this.AutoSize = true;
this.Controls.Add(this.textLabel);
}
void textLabel_SizeChanged(object sender, EventArgs e)
{
this.Height = this.textLabel.Bottom + 0;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.textLabel.MaximumSize = new Size(this.Width, 0);
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return this.textLabel.Text; }
set { this.textLabel.Text = value; }
}
}
方法 2:从头开始自动调整大小,无需标签的简单控制
这种方法的工作原理是基于设置控件的大小SetBoundsCore
基于当前宽度和计算出的高度Text
。计算控制高度。你可以简单地画出Image
.
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;
[Designer(typeof(MyLabelDesigner))]
public class ExLabel : Control
{
public ExLabel()
{
AutoSize = true;
DoubleBuffered = true;
SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size);
Invalidate();
}
protected override void SetBoundsCore(int x, int y, int width, int height,
BoundsSpecified specified)
{
var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak;
var proposedSize = new Size(width, int.MaxValue);
var size = TextRenderer.MeasureText(Text, Font, proposedSize, flags);
height = size.Height;
base.SetBoundsCore(x, y, width, height, specified);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak;
TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle,
ForeColor, BackColor, flags);
}
}
Designer
这里是ControlDesigner
对于这两种实现,它用于将设计器中的手柄大小限制为左侧和右侧:
using System.Windows.Forms.Design;
public class MyLabelDesigner : ControlDesigner
{
public override SelectionRules SelectionRules
{
get
{
return (base.SelectionRules & ~(SelectionRules.BottomSizeable |
SelectionRules.TopSizeable));
}
}
}