当用户绘制的控件大小增加时,新区域不会重新绘制 [英] New area not repaints when user-drawn control size is increased
问题描述
我想我这里缺少一些琐碎的东西。我直接从 Control
派生了简单的控件。我重写 OnPaint
并绘制矩形( e.Graphics.DrawRectangle
)和其中的文本( e.Graphics.DrawString
)。我没有覆盖任何其他成员。
I think I'm missing something trivial here. I derived simple control directly from Control
. I'm overriding OnPaint
and painting the rectangle (e.Graphics.DrawRectangle
)and a text inside it (e.Graphics.DrawString
). I did not override any other members.
当控件调整为较小尺寸时,它会很好地绘制自身,但是当控件调整为较大尺寸时,则新区域为没有正确地粉刷。一旦我再次将其大小调整为较小的大小,即使按一个像素,一切都将正确地重新绘制。
It paints itself well when the control is resized to the smaller size, but when it gets resized to the larger size, new area is not repainted properly. As soon as I resize it to the smaller size again, even if by one pixel, everything repaints correctly.
OnPaint
会被正确调用(将适当的 PaintEventArgs.ClipRectangle
正确设置为新区域),但是无论如何都不会绘制新区域(出现伪像)。
OnPaint
gets called properly (with appropriate PaintEventArgs.ClipRectangle
set correctly to new area), but the new area is not painted (artifacts appear) anyway.
我想念什么?
编辑:
代码:
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);
}
推荐答案
尝试将其添加到构造函数中:
Try adding this in your constructor:
public MyControl() {
this.ResizeRedraw = true;
this.DoubleBuffered = true;
}
在绘画活动中,清除上一个图形:
and in your paint event, clear the previous drawing:
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.Clear(SystemColors.Control);
// yada-yada-yada
}
这篇关于当用户绘制的控件大小增加时,新区域不会重新绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!