我创建了一个源自面板的自定义控件。我用它来使用BackgroundImage 属性显示图像。我重写 OnClick 方法并将 isSelected 设置为 true,然后调用 Invalidate 方法并在重写的 OnPaint 中绘制一个矩形。
一切都很顺利,直到我将 DoubleBuffered 设置为 true。绘制了矩形,然后将其删除,我不明白为什么会发生这种情况。
public CustomControl()
: base()
{
base.DoubleBuffered = true;
base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
PaintSelection();
}
private void PaintSelection()
{
if (isSelected)
{
Graphics graphics = CreateGraphics();
graphics.DrawRectangle(SelectionPen, DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1);
}
}
In your PaintSelection
,你不应该创建一个新的Graphics
对象,因为该对象将绘制到前缓冲区,然后该对象会立即被后缓冲区的内容透支。
油漆到Graphics
通过在PaintEventArgs
反而:
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
PaintSelection(pe.Graphics);
}
private void PaintSelection(Graphics graphics)
{
if (isSelected)
{
graphics.DrawRectangle(SelectionPen, DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)