Welcome to Dream.In.Code
Click Here
Getting C# Help is Easy!

Join 117,543 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,744 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Drawing a line on runtime

 
Reply to this topicStart new topic

Drawing a line on runtime, Drawing a line without erasing previous drawings

Areeb Mir
post 19 Jul, 2008 - 12:46 PM
Post #1


New D.I.C Head

*
Joined: 26 Feb, 2008
Posts: 3

Hi! I'm making a paint application and i can't figure out how to draw a new line or any other shape without clearing the previous drawings please help as soon as possible.


CODE


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;



namespace WindowsApplication3
{
    public partial class Form1 : Form
    {

        private void Form1_Load(object sender, EventArgs e)
        {

        }



        private Graphics DrawingSurface;

        private System.Drawing.Pen ErasePen;

        private System.Drawing.Pen DrawPen;

        private System.Drawing.Point ptsStart;

        private System.Drawing.Point ptsPrevious;

        
    
        public Form1()

        {

            InitializeComponent();


            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            SetStyle(ControlStyles.UserPaint, true);

            SetStyle(ControlStyles.ResizeRedraw, true);
            DrawingSurface = CreateGraphics();

            // Pen to erase a previous line

           ErasePen = new System.Drawing.Pen(BackColor);

            // Pen to draw a new line

            DrawPen = new System.Drawing.Pen(Color.Blue);

          

            //ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint, true);

        }



        protected override void OnMouseDown(MouseEventArgs e)

        {

            //Check if the left mouse button has

            //generated the associated event.

            if (e.Button == MouseButtons.Left)

            {

                //Store the starting point for your rubber-band line.

                ptsStart.X = e.X;

                ptsStart.Y = e.Y;

                //Store the previous endpoint for your rubber-band line.

                ptsPrevious = ptsStart;

            }

        }





        protected override void OnMouseMove(MouseEventArgs e)

        {

            //Check if the left mouse button is pressed.

            if (e.Button == MouseButtons.Left)

            {

                //Declare a local variable for the current endpoint '

                //of your rubber-band line.

                Point ptsCurrent = new Point();

                //Store the current endpoint of your rubber-band line.

                ptsCurrent.X = e.X;

                ptsCurrent.Y = e.Y;

                //Draw your actual rubber-band lines.

                DrawingSurface.DrawLine(ErasePen, ptsStart.X, ptsStart.Y,

                    ptsPrevious.X, ptsPrevious.Y);

                DrawingSurface.DrawLine(DrawPen, ptsStart.X, ptsStart.Y,

                    ptsCurrent.X, ptsCurrent.Y);
                

                //Store the current endpoint for the next call to OnMouseMove.

                ptsPrevious = ptsCurrent;

            }          

        }

    
    }

}
    
Attached File  Form1.txt ( 2.85k ) Number of downloads: 31
User is offlineProfile CardPM

Go to the top of the page


skaoth
post 20 Jul, 2008 - 12:14 AM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 333



Thanked 8 times

Dream Kudos: 100
My Contributions


It looks like what you need to do is put the pair of Point objects you create OnMouseMove() into an array or collection. then Instead of drawing the line inside of the OnMouseMove() handler, override the OnPaint() method. In this method cycle through the array or collection and draw the line.

I'm on linux box right now so I apologize for any mistakes below
smile.gif I'll just say this is pseudo code

CODE

public class Line
{
        private Point m_start;
        private Point m_end;

        public Line(Point start, Point end)
        {
                m_start = start;
                m_end = end;
        }

        public void Draw(Graphic g)
        {
                // Your drawing code to draw
                // a line
        }
};


// The in your mousemove() or mouseup()
protected override void OnMouseMove(MouseEventArgs e)
{
        Point currentPoint = new Point();
        currentPoint.X = e.X;
        currentPoint.Y = e.Y;

        Line l = new Line(previousPoint, currentPoint);

        // TODO: Add the line object to the collection
}

protected override void OnPaint(...)
{
        // TODO: iterate over the list of line objects
        // and pass it in the graphics object to draw on the form
}


This post has been edited by skaoth: 20 Jul, 2008 - 12:15 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 05:44PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month