具有自动高度属性的用户控件

我想创建将显示文本的用户控件。
我需要一种在运行时调整控件大小的方法,以便调整高度以显示所有文本。 我已经创建了如下所示的控件:

在此处输入图像描述

正如您所看到的,我使用TextRenderer.DrawText绘制了图标和文本。 不幸的是,当我调整控件的大小(仅向左或向右)时,我的文本有时被剪切,如下所示:

在此处输入图像描述

我正在使用TextRenderer.MeasureText测量文本,因为我正在计算行数,然后我正在绘制该文本。 这是我正在使用的代码:

 [Designer(typeof(MyTextBoxDesigner))] public partial class MyTextBox : UserControl { public MyTextBox() { InitializeComponent(); } [DefaultValue("Demo"), Description("Text of control"), Category("Appearance"),Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override string Text { get { return base.Text; } set { if (base.Text == value) return; base.Text = value; Invalidate(); } } Image _image; [Description("Image shown on the left side of the control"), Category("Appearance")] public Image Image { get { return _image; } set { if (_image == value) return; _image = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { int imagePadding = 0; if (_image != null) { imagePadding = 25; e.Graphics.DrawImage(_image,new Rectangle(5,5,16,16)); } Size textSize1 = TextRenderer.MeasureText(e.Graphics, Text, Font); SizeF textSize2 = e.Graphics.MeasureString(Text, Font); Debug.WriteLine(textSize2); int maxTextWidth = Width - Padding.Left-Padding.Right-imagePadding; int lineHeight = textSize1.Height + 2; int numLines = 1; if (textSize1.Width > maxTextWidth) { numLines = textSize1.Width / maxTextWidth + 1; } Rectangle textRect = new Rectangle { Width = Width - Padding.Left-Padding.Right-imagePadding, Height = (numLines * lineHeight), X = Padding.Left+imagePadding, Y = 5 }; TextRenderer.DrawText(e.Graphics, Text, Font, textRect, ForeColor, TextFormatFlags.WordBreak | TextFormatFlags.Left | TextFormatFlags.Top); e.Graphics.DrawRectangle(Pens.CadetBlue, textRect); base.OnPaint(e); } } internal class MyTextBoxDesigner : ControlDesigner { public override SelectionRules SelectionRules { get { //all minus bottom and top return (base.SelectionRules & ~(SelectionRules.BottomSizeable | SelectionRules.TopSizeable)); } } } 

理想情况下,我希望我的控制能够根据文本长度自动调整高度,因此如果文本很短(或控制时间足够长),例如20px高度,但如果文本很长,我的控制应该是(例如)40px高度(2行文字)。

如何更改文本测量以正确计算textRect
我应该使用Size属性更新我的控件的高度还是有更好的方法?

编辑:基本上我想绘制可以包裹在矩形内部但具有固定宽度的文本,因此当文本较长时,矩形将调整其高度。 然后根据那个高度,我想调整控制高度。

这是一个自动高度控制。 如果更改控件的宽度,控件的高度将以可显示整个文本的方式更改。

在此处输入图像描述

您可以使用不同的方法创建此类控件,包括:

  • 方法1:托管标签的自动尺寸复合控制
    此方法基于在自动大小Control托管具有动态最大宽度的自动大小Label 。 在这种方法中,我们根据控件的宽度设置标签的最大宽度,因为标签是自动尺寸,它的高度将自动设置为显示所有文本,然后我们根据标签的高度设置控制高度。

  • 方法2:自动调整简单控制从头开始,不带标签
    此方法基于覆盖SetBoundsCore并基于其Text大小设置控件大小。 在这种方法中,我们使用TextRenderer.MeasureText基于控件的宽度计算文本的大小,然后将计算的高度设置为控件的高度。 在这种方法中,您应该处理文本格式标志并自己渲染。

在这两种方法中, ControlDesigner用于禁用除左右之外的所有尺寸抓握手柄。

请注意

这些不是唯一可用的方法,但却是很好的例子。 作为另一种选择,您可以从Labelinheritance并更改其行为。

方法1:托管标签的自动尺寸复合控制

它的工作原理是将label的AutoSize属性设置为true ,然后根据控制Width设置标签的MaximumSize 。 控制高度也根据标签的高度设定。 您只需在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); } } 

设计师

以下是ControlDesigner ,它用于限制设计器中的左侧和右侧的大小抓握手柄,以实现两种实现:

 using System.Windows.Forms.Design; public class MyLabelDesigner : ControlDesigner { public override SelectionRules SelectionRules { get { return (base.SelectionRules & ~(SelectionRules.BottomSizeable | SelectionRules.TopSizeable)); } } }