help with example "paint on form by mouse"

S

Serge Klokov

Hi!

1. Please, help with example "paint on form by mouse"
2. Below is my example, but it clear the line after each Refresh()... how to
fix?
3. How to draw the line in Mouse_Move event?


private Boolean isCanPaint = false;
private MouseEventArgs MouseEvent1;
private MouseEventArgs MouseEvent2;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent1 = e;
label1.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
}


private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent2 = e;
label2.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
isCanPaint = true;
Refresh();
}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (isCanPaint)
{
Pen myPen = new Pen(Color.Blue, 3);
e.Graphics.DrawLine(myPen,MouseEvent1.X, MouseEvent1.Y, MouseEvent2.X,
MouseEvent2.Y);
}
}
 
B

Bob Powell [MVP]

After my signature you'll find a scribble like application that handles
mouse, click and paint events to create a scribble like program.

Have fun...

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml

----------------------------------------------------------------------------
-----------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace Scribbler

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

//array used to store arrays of points.

ArrayList items=new ArrayList();

//The line under construction.

ArrayList line;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

this.SetStyle(ControlStyles.AllPaintingInWmPaint |

ControlStyles.UserPaint |

ControlStyles.DoubleBuffer,

true);

}



/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(800, 600);

this.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseUp);

this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);

this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseDown);

this.Paint += new System.Windows.Forms.PaintEventHandler(this.form1_Paint);

this.Name = "Form1";

this.Text = "Form1";

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

//a semaphore used to store the state of the mouse button

bool _drawing;

//Draws the lines in the items array and the one under construction.

private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)

{

foreach(Point[] pa in items)

{

if(pa.Length>0)

{

if(pa.Length==1)

{

e.Graphics.DrawLine(Pens.Black,pa[0],pa[0]);

}

else

e.Graphics.DrawLines(Pens.Black,pa);

}

}

//If a new line is under construction draw it too for the benefit of the
user.

if(_drawing)

{

if(line.Count>1)

{

Point[] pts=new Point[line.Count];

line.CopyTo(pts,0);

e.Graphics.DrawLines(Pens.Black,pts);

}

}

}


//Begins drawing a new line

private void form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)

{

_drawing=true;

line=new ArrayList();

}

//When drawing a line this accumulates points in the line array

//Note that no drawing is performed.

private void form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)

{

if(_drawing)

{

this.line.Add(new Point(e.X,e.Y));

Invalidate();

}

}

//Ends accumulating a new line by creating a new point array and storing it
with the ones already completed

private void form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)

{

_drawing=false;

line.Add(new Point(e.X,e.Y));

Point[] pts=new Point[line.Count];

line.CopyTo(pts,0);

items.Add(pts);

Invalidate();

}

}

}
 
W

Whidbey

Hi!

Method's:

1. Using ArrayList of points for save and draw
2. Using class GraphicsPath for make figure
3. Using "shadow bitmap"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top