【发布时间】:2016-09-11 05:33:17
【问题描述】:
我想我在这里遗漏了一些微不足道的东西。我直接从Control 派生了简单的控制。我将覆盖OnPaint 并绘制矩形(e.Graphics.DrawRectangle)和其中的文本(e.Graphics.DrawString)。我没有覆盖任何其他成员。
当控件被调整为较小的尺寸时,它会很好地绘制自己,但是当它被调整为较大的尺寸时,新区域不会正确地重新绘制。一旦我再次将其调整为较小的尺寸,即使缩小一个像素,一切都会正确重绘。
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);
}
【问题讨论】:
-
能否请您发布您正在使用的代码(
OnPaint)来绘制控件?