使用AutoSize标签的问题是Tahoma字体使用选项TextRenderingHint.ClearTypeGridFit绘制的。 没有ClearTypeGridFit它看起来没问题,但是 - 它被父容器裁剪(参见附图:第一个标签','被裁剪)
我发现它只有Tahoma字体。
自定义标签代码:
class CustomLabel: Label
{
public CustomLabel ()
:base()
{
}
protected override void OnPaint ( PaintEventArgs e )
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
base.OnPaint( e );
}
}
来自设计师档案的代码和平:
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
this.label1.Location = new System.Drawing.Point( 54, 95 );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size( 35, 13 );
this.label1.TabIndex = 1;
this.label1.Text = resources.GetString( "label1.Text" );
//
// customLabel1
//
this.customLabel1.AutoSize = true;
this.customLabel1.BackColor = System.Drawing.Color.NavajoWhite;
this.customLabel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.customLabel1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
this.customLabel1.ForeColor = System.Drawing.Color.Black;
this.customLabel1.Location = new System.Drawing.Point( 1, 1 );
this.customLabel1.Margin = new System.Windows.Forms.Padding( 0 );
this.customLabel1.Name = "customLabel1";
this.customLabel1.Size = new System.Drawing.Size( 192, 154 );
this.customLabel1.TabIndex = 0;
this.customLabel1.Text = resources.GetString( "customLabel1.Text" );
我发现只是重写了一个GetPreferedSize函数:
public override Size GetPreferredSize ( Size theProposedSize )
{
if ( TextRendering == TextRenderingHint.ClearTypeGridFit )
{
Graphics aGraphics = Graphics.FromHwnd( this.Handle );
if ( aGraphics != null )
{
aGraphics.TextRenderingHint = theTextRendering;
Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
//apply control minimum size
aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );
return aResult;
}
}
return base.GetPreferredSize( theProposedSize );
}
但是我无法对结果应用最大大小。 为什么我需要最大尺寸?例如,我希望有可能按宽度限制标签。
有什么想法吗?
答案 0 :(得分:0)
所以,我的最终解决方案看起来如此:
public override Size GetPreferredSize ( Size theProposedSize )
{
if ( SystemInfo.WinVersion == SystemInfo.Version.WindowsXP )
{
if ( theTextRendering == TextRenderingHint.ClearTypeGridFit )
{
Graphics aGraphics = Graphics.FromHwnd( this.Handle );
if ( aGraphics != null )
{
aGraphics.TextRenderingHint = theTextRendering;
Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
//apply padding and margin
aResult.Width += Margin.Horizontal + Padding.Horizontal;
aResult.Height += Margin.Vertical + Padding.Vertical;
//apply control minimum size
aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );
//adopt maximum width
if ( MaximumSize.Width > 0 )
{
while ( aResult.Width > MaximumSize.Width )
{
aResult.Width -= MaximumSize.Width;
aResult.Height += Font.Height;
if ( aResult.Width < MaximumSize.Width )
{
aResult.Width = MaximumSize.Width;
}
}
}
return aResult;
}
}
}
return base.GetPreferredSize( theProposedSize );
}