私はここで些細なことを見逃していると思います。から直接単純な制御を導き出しましたControl
。OnPaint
長方形(e.Graphics.DrawRectangle
)とその中のテキスト()をオーバーライドしてペイントしていますe.Graphics.DrawString
。他のメンバーをオーバーライドしませんでした。
コントロールを小さいサイズにサイズ変更するとうまくペイントされますが、大きいサイズにサイズ変更すると、新しい領域が適切に再ペイントされません。もう一度小さいサイズにサイズ変更するとすぐに、たとえ1ピクセルでも、すべてが正しく再描画されます。
OnPaint
は適切に呼び出されますが(適切PaintEventArgs.ClipRectangle
に新しい領域に正しく設定されている場合)、新しい領域はペイントされません(アーティファクトが表示されます)。
私は何が欠けていますか?
編集:
コード:
protected override void OnPaint(PaintEventArgs e)
{
// Adjust control's height based on current width, to fit current text:
base.Height = _GetFittingHeight(e.Graphics, base.Width);
// Draw frame (if available):
if (FrameThickness != 0)
{
e.Graphics.DrawRectangle(new Pen(FrameColor, FrameThickness),
FrameThickness / 2, FrameThickness / 2, base.Width - FrameThickness, base.Height - FrameThickness);
}
// Draw string:
e.Graphics.DrawString(base.Text, base.Font, new SolidBrush(base.ForeColor), new RectangleF(0, 0, base.Width, base.Height));
}
private int _GetFittingHeight(Graphics graphics, int width)
{
return (int)Math.Ceiling(graphics.MeasureString(base.Text, base.Font, width).Height);
}