長方形内のテキストを測定したいのですが、フォーマット フラグに TextFormatFlags.SingleLine が含まれている場合にのみ正しく機能します。
例えば。フォームにパネルを配置し、この Paint イベント ハンドラーを追加します。
private void panel1_Paint(object sender, PaintEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
Panel panel = (sender as Panel);
//Draw the text
TextRenderer.DrawText(e.Graphics, "hello", SystemFonts.DefaultFont, new Rectangle(0, 0, panel.Bounds.Width, panel.Bounds.Height), Color.Red, flags);
//Draw a border around the panel
e.Graphics.DrawRectangle(System.Drawing.Pens.Black, 0, 0, panel.Width - 1, panel.Height - 1);
//Measure the text and draw a rect around it
Size s = TextRenderer.MeasureText(e.Graphics, "hello", SystemFonts.DefaultFont, new Size(panel.Bounds.Width, panel.Bounds.Height), flags);
e.Graphics.DrawRectangle(Pens.Blue, 0, 0, s.Width, s.Height);
}
あなたはこれを見るでしょう
ただし、フラグで SingleLine を取り出した場合。
TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
次に、これを取得します
動作が仕様によるものだとは思いませんが、いずれにせよ、簡単に回避できますか?
ありがとう