• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Paint Event problem

I've drawn some circles on a form, but when i minimize the form and maximize it, i cannot see the circles which i've drawn. How can i prevent it?

Someone please help me...
0
nandithaa
Asked:
nandithaa
1 Solution
 
CuteBugCommented:
You have to subscribe to the Paint event of the form and perform your drawing in the handler

When you minimize the form and maximize it, the Form repaints itself, thus the circle which you had drawn earlier will be erased.

 private void Form1_Paint(object sender, PaintEventArgs e)
{
    // perform your drawing here...
    e.Graphics.DrawEllipse(...);
}

Open in new window

0
 
ajitha75Commented:
When you minimize and maximize, the Form gets repainted. To get your circles back,you need to add that code in OnPaint event handler of the form.

Thanks
Ajitha
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Totally agree with the previous posts.

Make sure that you are using the Graphics supplied for you in the Paint() event via "e.Graphics" as CuteBug has shown.

Do NOT use CreateGraphics()!....
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
TribmosCommented:
Another method is to have a Bitmap object  which you draw on, then have the paint method take that bitmap and draw it on the form.

e.Graphics.DrawImage(...);

Just remember to Dispose of a previous bitmap if you create a new one (say from resizing or any other reason)  Setting a Bitmap object to null will not immediatly free up the memory it allocated.
0
 
nandithaaAuthor Commented:
I'm drawing the image in MouseClick event using the method CreateGraphics(), because i need to get the X Y position to draw circle. User can draw as many circles as he needs, on the form.
How can i prevent the repainting of Paint event? So that while the user minimize and maximize the form, the circles drawn on the form does not disappear.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to store the info about the clicks in a persistent data structure and redraw ALL of them in the Paint() Event.

Simple example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            this.MouseClick += new MouseEventHandler(Form1_MouseClick);
            this.Paint += new PaintEventHandler(Form1_Paint);
        }

        private const int CircleSize = 25;
        private List<Point> circles = new List<Point>();

        void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            Point pt = this.PointToClient(Cursor.Position);
            circles.Add(pt);
            this.Refresh();
        }

        void Form1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rc;
            foreach (Point pt in circles)
            {
                rc = new Rectangle(pt, new Size(1, 1));
                rc.Inflate(CircleSize, CircleSize);
                e.Graphics.DrawEllipse(Pens.Black, rc);
            }
        }

    }
}

Open in new window

0
 
nandithaaAuthor Commented:
That worked.... Thanks a lot
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now