【发布时间】:2012-05-12 12:30:54
【问题描述】:
我有一个闪烁非常糟糕的 Winform 用户控件。控件的功能运行良好。它只是闪烁真的很糟糕。我在位图上进行所有绘图,然后使用DrawImage 将位图复制到屏幕上,所以我对闪烁的程度感到惊讶。以下是我所拥有的摘录:
private void ScrollPanel_Paint(object sender, PaintEventArgs e)
{
var c = (Calendar)Parent;
Bitmap bmp = c.RequestImage();
if (bmp == null)
return;
e.Graphics.DrawImage(bmp, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height),
new Rectangle(0, _scrollOffset, ClientSize.Width, ClientSize.Height),
GraphicsUnit.Pixel);
_bmpSize = bmp.Height;
e.Graphics.Dispose();
bmp.Dispose();
}
private void ScrollPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_oldMouseCoords = e.Location;
}
}
private void ScrollPanel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
_mouseDown = false;
}
private void ScrollPanel_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown && e.Location.Y < _oldMouseCoords.Y && _scrollOffset < _bmpSize - _scrollOffset - ClientSize.Height)
{
int offset = _oldMouseCoords.Y - e.Location.Y;
_scrollOffset += offset;
Refresh();
}
if (_mouseDown && e.Location.Y > _oldMouseCoords.Y && _scrollOffset > 0)
{
int offset = e.Location.Y - _oldMouseCoords.Y;
_scrollOffset -= offset;
Refresh();
}
_oldMouseCoords = e.Location;
}
它应该做的是,当我用鼠标拖动时,它应该滚动位图,它就是这样。就像我说的那样,功能一切正常。从Paint 事件中可以看出,我所做的只是获取我的位图,然后将其直接复制到屏幕上。
任何帮助将不胜感激。
【问题讨论】:
-
尝试将表单的属性 DoubleBuffered 设置为 true
-
Yorye -- 我试过了,但是每当我在 Winform 应用程序中这样做时,我总是在运行时收到一个错误,上面写着
Invalid Parameter -
我认为你不应该在
Graphics对象上调用Dispose()......这不是你要处理的,并且在你完成绘画之后控件可能仍然需要它。 -
是的,布拉德利。那是正确的。我删除了对图形对象的 Dispose() 调用,现在我可以对它进行 DoubleBuffer。
-
Yorye -- 双缓冲解决了我的闪烁问题。将您的评论变成答案,我将使其正确。
标签: c# winforms paint flicker drawimage