オプション TextRenderingHint.ClearTypeGridFit を使用して Tahoma フォントで描画されるラベルに AutoSize を使用する際の問題です。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 );
}
しかし、結果に最大サイズを適用できません。なぜ最大サイズが必要なのですか? たとえば、ラベルを幅で制限できるようにしたいと考えています。
何か案は?