对于 System.Windows.Forms.Label
是有办法来自动调整取决于标签尺寸标签字体大小?
类AutoFontLabel:标签
{
公共AutoFontLabel()
: 基础()
{
this.AutoEllipsis = TRUE;
}
保护覆盖无效OnPaddingChanged(EventArgs的发送)
{
UpdateFontSize();
base.OnPaddingChanged(E);
}
保护覆盖无效onResize受到(EventArgs的发送)
{
UpdateFontSize();
base.OnResize(E);
}
私人无效UpdateFontSize()
{
INT textHeight不同= this.ClientRectangle.Height
- this.Padding.Top - this.Padding.Bottom;
如果(textHeight不同大于0)
{
this.Font =新的字体(this.Font.FontFamily,
textHeight不同,GraphicsUnit.Pixel);
}
}
}
由于AMissico的更新控制处理填充。我们可以看到如何改变填充和textAlign设置的affectd的设计师。
For a System.Windows.Forms.Label
is there a way to auto-fit the label font size depending on the label size?
class AutoFontLabel : Label
{
public AutoFontLabel()
: base()
{
this.AutoEllipsis = true;
}
protected override void OnPaddingChanged(EventArgs e)
{
UpdateFontSize();
base.OnPaddingChanged(e);
}
protected override void OnResize(EventArgs e)
{
UpdateFontSize();
base.OnResize(e);
}
private void UpdateFontSize()
{
int textHeight = this.ClientRectangle.Height
- this.Padding.Top - this.Padding.Bottom;
if (textHeight > 0)
{
this.Font = new Font(this.Font.FontFamily,
textHeight, GraphicsUnit.Pixel);
}
}
}
Thanks to AMissico that updated the control to handle padding. We can see how changing the Padding and TextAlign are affectd in the designer.