Click here to Skip to main content
15,915,324 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Experts!
I am working on drawing in C#. I have a problem in this program; when I draw a new object on mouse event and paint event the previous drawn object disappears. My code is as follows.

C#
private void picMain_MouseDown(object sender, MouseEventArgs e)
        {
            MouseDownPoint = e.Location;
            mouseMovePoint = e.Location;
        }

private void picMain_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMovePoint = e.Location;
        }
private void picMain_Paint(object sender, PaintEventArgs e)
        {
            G.DrawLine(new Pen(Color.Green, 10),MouseDownPoint, mouseMovePoint);
        }

Please help me to draw an unlimited number of objects without erasing the previous one.
Posted
Updated 19-Mar-10 6:10am
v2

kachura wrote:
when I draw a new object on mouse event and paint event the previous drawn object disappears.


This is how Windows works. Each time the paint event occurs you have to re-paint from scratch. Your best bet would be storing the points in a container (appending on mouse events) and then drawing all of them in the paint handler.
:)
 
Share this answer
 
v2
It's because you've hooked the Paint event. It will only draw the one line on your paint event. The hard way to do this would be to create an array to store all of the lines and then iterate through the array on the Paint event.

The simpler way is to hook the MouseUp instead and just draw the new line on the MouseUp. Doing that, you also don't need to hook the MouseMove as you'll have the end position already.
 
Share this answer
 
Hi,

This is a simple example of an application doing roughly what you want (note that this is just to get you started, there are better ways of drawing in C#);

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        IList<Point> points = new List<Point>();

        public Form1()
        {
            this.MouseClick += new MouseEventHandler(Form1_MouseClick);
        }

        void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            points.Add(e.Location);
            this.Invalidate(); // This causes the form to repaint
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Point previousPoint = Point.Empty;
            foreach (Point point in points)
            {
                if (previousPoint != Point.Empty)
                {
                    e.Graphics.DrawLine(Pens.Black, previousPoint, point);
                }
                previousPoint = point;
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 
Share this answer
 
The object in my case may be any drawing object e.g rectanlge, ellipse, line,polygon etc. is there any method to store all the drawn object in arraylist or list. if so then please guide me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900