3

I'm working on a small painting program similar to ms paint. At the moment, I'm trying to implement 'select function'. I'm facing flickering problem so I've did some researches and I found out, that I should create my own Panel class.

public class MyDisplay : Panel
    {   
        public MyDisplay()
        {
            this.DoubleBuffered = true;            

            this.SetStyle(ControlStyles.UserPaint |
              ControlStyles.AllPaintingInWmPaint |
              ControlStyles.ResizeRedraw |
              ControlStyles.ContainerControl |
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.SupportsTransparentBackColor
              , true);

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();
        }
    }

In main form there are fields:

MyDisplay panel1 = new MyDisplay();
Graphics graphics1 = panel1.CreateGraphics();

I use 3 events on a panel:

  1. MouseDown - I get here Point p1
  2. MouseMove - thats, where I get flickering problem, I'm calling graphics1.drawRectangle(...) and graphics1.Clear() everytime clicked mouse moves
  3. MouseUp - I just draw rectangle for the last time.

What's wrong with that? Why do I still face flickering problem even though whole panel is white and theres only 1 rectangle in there? Thank you.

edit:

I've overwrote OnPaint method but I still don't know what to do next.

   protected override void OnPaint(PaintEventArgs e)
    {
        // Call the OnPaint method of the base class.
        base.OnPaint(e);
        // Call methods of the System.Drawing.Graphics object.
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle);
    } 

edit2: Should I paint on bitmap/image and override OnPaint method to copy image from there and paste it to panel?

Patryk
  • 3,042
  • 11
  • 41
  • 83
  • 1
    You are destroying the double-buffered feature by using CreateGraphics(). You **must** override the OnPaint() method to do the painting and avoid flicker. – Hans Passant Mar 16 '13 at 16:12
  • do You mind giving me some tip on using OnPaint thing? I've got no clue how should that work, I've been trying to find that on google with no results – Patryk Mar 16 '13 at 16:35

1 Answers1

1

Delete the line defining the graphics1 field.

Perform ALL painting in the override of OnPaint, using the Graphics object passed in with the PaintEventArgs object. Use the methods Invalidate(), Refresh(), and Update() to control the timing of repainting from other code.

Callback if you encounter any specific difficulties with this design.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
  • could you give me some example? I'm not sure how should it look like, what If i want to draw rectangle and circle, shall I place 2 of those methods in OnPaint? – Patryk Mar 16 '13 at 22:44
  • Should I paint on bitmap/image and override OnPaint method to copy image from there and paste it to panel? – Patryk Mar 17 '13 at 00:46
  • Drawing on the bitmap, and drawing the bitmap to the screen, are usually both done from the OnPaint method. – Pieter Geerkens Mar 17 '13 at 02:56