I have a Winform user control that is flickering really bad. The functionality of the control is working just perfectly. Its just flickering really bad. I am doing all the drawing onto a bitmap then using DrawImage
to just copy the bitmap onto the screen, so I am surprised by how much flickering is occurring. Here is an excerpt of what I have:
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;
}
What its supposed to be doing is, when I drag with my mouse, it should be scrolling the bitmap, which it is. Like I said, the functionality is all working. As you can see from the Paint
event, all I'm doing is acquiring my bitmap then copying it directly to the screen.
Any help would be appreciated.