Search code examples
c#graphicscustom-controlswindows-ce

How to selective redraw shapes keeping other intact


I am working on Window CE application with custom control.

In custom control I am drawing three circles (One circle filled), one arrowhead one text and one line manually. It is like a angle selector in Photoshop.

Now when I move the cursor complete control redraw and cause a flickering affect.

I have tried drawing controls to bitmap first then in last draw this bitmap using e.drawimage() method but same flickering problem.

Now I have only 2 shapes(arrowhead+line) that needs redraw, others are with static behaviour.

So my question is there any way to selective draw shapes keeping other intact. Draw all shapes first time but just draw the arrowhead and line after that keeping all other shapes at there location intact.

This is what I am doing in

onPiant override method.

using (SolidBrush inLineFill = new SolidBrush(circleColor))
{
  Point[]  polyPoints = ...;                              
  e.Graphics.DrawLine(....);
  e.Graphics.FillPolygon(inLineFill, polyPoints);
  if (firstTime)
  {
    e.Graphics.DrawEllipse(...);
    e.Graphics.DrawEllipse(...);
    e.Graphics.DrawEllipse(...);
    e.Graphics.FillEllipse(...);
    e.Graphics.DrawString(...);
    firstTime = false ;
  }
}

I see complete control first time but I only see the arrowhead with line after that.


Solution

  • There are probably several things you can do to improve the behavior.

    • Override OnPaintBackground and leave it empty
    • Cache that SolidBrush instead of creating a new one every time OnPaint is called
    • Draw all of the shapes that are static to a member-level Bitmap that you cache. In OnPaint, do a DrawImage of that image then your arrowhead and line (the changing stuff).
    • Do all of this drawing to a Bitmap (that you cache, not create every time), then DrawImage that to the screen graphics
    • If you don't have overlapping stuff and you've got reasonable rectangular regions, setting a clipping area to surround your changes will prevent unnecessary drawing