我有一个闪烁非常糟糕的 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
事件中可以看出,我所做的只是获取我的位图,然后将其直接复制到屏幕上。
任何帮助,将不胜感激。