Custom control flickers when resizing diagonally. Double-buffering doesn't seem to help

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
Im writing a Custom Control (inherits UserControl) which contains a Panel (name "drawPanel") where content will be rendered. Ive created a handler for drawPanels Paint event, and Im doing my drawing there. I draw everything in a Bitmap first, and then call the drawPanel graphics DrawImageUnscaled function to paint the bitmap on the panel.
When resizing the form which contains my control vertically or horizontally, the control repaints perfectly well. But when I resize the form diagonally using the corner grips of the form, it flickers.
Any suggestions? Below is the Paint handler code.
Please note Ive already tried, but without success, the following:
  • Use a BackBuffer.
  • Set the DoubleBuffered property of the custom control and the containing Form to TRUE.
  • Susped/Resume Layout upon containing Forms ResizeBegin/ResizeEnd events (this causes the redraw to feel "weird" and delayed).
  • Overriden the custom controls OnPaintBackground method so that it doesnt call the base method.
Thanks in advance. public void redrawTopPanel(PaintEventArgs e)
{
Bitmap b = new Bitmap(drawPanel.Width,drawPanel.Height);
Graphics g = System.Drawing.Graphics.FromImage(b);

int stopX = e.ClipRectangle.Right;
int stopY = e.ClipRectangle.Bottom;
int boxW = charW + hSpacing;
int boxH = charH + vSpacing;
int startX = ((int) (e.ClipRectangle.X / boxW)) * boxW;
int startY = ((int) (e.ClipRectangle.Y / boxH)) * boxH;

Random n = new Random();
Color c1 = Color.Blue;
Color c2 = Color.White;
Brush br = new SolidBrush(c2);

for (int x = startX; x <= e.ClipRectangle.Right; x += boxW)
{
for (int y = startY; y <= e.ClipRectangle.Bottom; y += boxH)
{
c1 = System.Drawing.Color.FromArgb(n.Next(0, 255), 128, 128);
g.DrawRectangle(new Pen(c1), x, y, charW, charH);
g.FillRectangle(br, x + 2, y + 2, charW - 3, charH - 3);
}
}
WriteLn(String.Format("Redrawing [x,y,x,y] = [{0},{1},{2},{3}]", e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Right, e.ClipRectangle.Bottom));
Graphics dg = drawPanel.CreateGraphics();
dg.DrawImageUnscaled(b, 0, 0);
}

View the full article
 
Back
Top